繁体   English   中英

分配随机数C#

[英]Assigning random numbers c#

我想做的是检索随机的“ x”和“ y”值,以将它们分配给我的每个陷阱。 我已经学会了如何在特定范围内获取随机数,但是我尝试了各种方法来检索第一个随机xvalue和yvalue进行分配,但是我收到了“ System.Random”。

编辑:谢谢大家的答案和帮助,但我仍然不清楚我如何获得随机值并分配它们。 我想做的是,如果我将3作为陷阱的数量,程序将给出6个值,x坐标为3,y为3,我无法弄清楚的是我如何得到它们。 例如。 x的RNG值为(4,7,1),y值为(8,1,6)。 我试图使它成为trap1的值(4,8)分配给它trap2(7,1)和trap3(1,6)。 任何指导或技巧都会有所帮助。

    Console.WriteLine ("Please enter the number of traps.");

    Random rndX = new Random ();
    Random rndY = new Random ();
    int traps;

    traps = Convert.ToInt32 (Console.ReadLine ());
    Console.WriteLine ("X coordinates for the traps", traps);

    for (int X = 1; X <= traps; X++) 
    {
        Console.Write ("{0}", rndX.Next (1, 11)); 
        Console.WriteLine ();
    }

    Console.WriteLine ("Y coordinates for the traps");
    for (int Y = 1; Y <= traps; Y++) 
    {
        Console.Write ("{0}", rndY.Next (1, 11));
        Console.WriteLine ();
    }

我尝试检索随机值的内容

    Console.WriteLine ("{0,0}",rndX, rndY);

谢谢您查看此:D任何有关如何进行此操作的提示或指南,将不胜感激

您需要使用相同的随机数生成器,C#中的RNG是无效算法,并且默认情况下还使用时间戳种子实现,因此很可能两者都可能:

rndX.Next()
rndY.Next()

由于指令是如此之快,因此它们将返回与使用相同的时间戳创建的相同数字。 而是两次调用同一RNG,并使用数组索引选择一个值,如下所示:

var arrX= new int[] {1,2,3};
var arrY = new int[] { 6,7,8};
Console.WriteLine ("{0},{1}",arrX[rndX.Next(3)], arrY[rndX.Next(3))];

这是错误的,它在随机类上调用ToString()方法,因此输出System.Random

Console.WriteLine ("{0,0}",rndX, rndY);

您需要使用{0}{1}作为第一个和第二个值来正确索引这些值。

Console.WriteLine ("{0}, {1}",rndX.Next(1, 11), rndY.Next(1, 11));

您必须像在代码另一部分中一样调用Next()方法。

最后一行代码在您的Random类实例上隐式调用ToString() ,该实例输出类类型(本例中为"System.Random" )。

尝试这个

Console.WriteLine ("{0},{1}",rndX.Next(), rndY.Next());

一旦使用这些值,除非您保存它们,否则它们将被丢弃。

Console.WriteLine ("Please enter the number of traps.");

Random rnd = new Random ();
int traps;

traps = Convert.ToInt32 (Console.ReadLine ());
Console.WriteLine ("X coordinates for the traps", traps);

for (int X = 1; X <= traps; X++) 
{
    int rx = rnd.Next (1, 11);
    Console.Write ("{0}", rx); 
    Console.WriteLine ();
}

Console.WriteLine ("Y coordinates for the traps");
for (int Y = 1; Y <= traps; Y++) 
{
    int ry = rnd.Next (1, 11);
    Console.Write ("{0}", ry); 
    Console.WriteLine ();
}

同样,您只需要一个随机对象。

由于您是在循环中检索值,因此您应该查看单个值的类和用于存储这些值集合的List。

更新:

这是使用类和列表的示例:

public class Trap
{
    public int X = 0;
    public int Y = 0;
}

Console.WriteLine("Please enter the number of traps.");

Random rnd = new Random();
int amount;
List<Trap> traps = new List<Trap>();
amount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Coordinates for {0} traps", amount);
for(int i = 0; i < amount; i++)
{
    Trap temp = new Trap();
    temp.X = rnd.Next(1, 11);
    temp.Y = rnd.Next(1, 11);
    Console.WriteLine("Coordinates for Trap {0} - {1},{2}", i + 1, temp.X, temp.Y);
    traps.Add(temp);
}

每个陷阱坐标都在列表traps (例如,坐标1是traps[0]

您是否尝试过以下代码:

 public string GetRandomNumber(Int32 digit)
{
    if (digit < 0) digit *= -1;
    if (digit == 0) digit = 1;
    Random rn = new Random();
    string r = "";
    for (int i = 1; i <= digit; i++)
    {
        r += "9";
    }
    return rn.Next(Convert.ToInt32(r)).ToString();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM