简体   繁体   English

分配随机数C#

[英]Assigning random numbers c#

what I am trying to do is to retrieve the random "x" and "y" values to assign them to each of my traps. 我想做的是检索随机的“ x”和“ y”值,以将它们分配给我的每个陷阱。 I've learned how to get the random numbers within a specific range but I have tried various ways to retrieve the 1st random xvalue and yvalue to assign them but I receive "System.Random". 我已经学会了如何在特定范围内获取随机数,但是我尝试了各种方法来检索第一个随机xvalue和yvalue进行分配,但是我收到了“ System.Random”。

EDIT: Thank you all for the answers and help but im still unclear on how i can get the random values and assign them. 编辑:谢谢大家的答案和帮助,但我仍然不清楚我如何获得随机值并分配它们。 What im trying to do is if i put 3 as the number of traps the program would give 6 values, 3for x coordinate and 3 for y, what im having trouble figuring out is how i get them. 我想做的是,如果我将3作为陷阱的数量,程序将给出6个值,x坐标为3,y为3,我无法弄清楚的是我如何得到它们。 Eg. 例如。 RNG values for x are (4,7,1) and the y values are (8,1,6). x的RNG值为(4,7,1),y值为(8,1,6)。 im trying to make it so that trap1 would have the values (4,8) assigned to it trap2 (7,1) and trap3 (1,6). 我试图使它成为trap1的值(4,8)分配给它trap2(7,1)和trap3(1,6)。 any guidance or tips would help. 任何指导或技巧都会有所帮助。

    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 ();
    }

What I've tried to retrieve the random values 我尝试检索随机值的内容

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

thank you for looking over this :D any tips or guides into how i should do this would be greatly appreciated 谢谢您查看此:D任何有关如何进行此操作的提示或指南,将不胜感激

You need to use the same random number generator, the RNG in C# is a busted algorithm, and is also implemented with a time stamp seed as a default, so it can easily be possible that both: 您需要使用相同的随机数生成器,C#中的RNG是无效算法,并且默认情况下还使用时间戳种子实现,因此很可能两者都可能:

rndX.Next()
rndY.Next()

will return the same number as they are both created with effectively the same time stamp due to instructions being so fast. 由于指令是如此之快,因此它们将返回与使用相同的时间戳创建的相同数字。 Instead call the same RNG twice,and use array indexing to select a value, like so: 而是两次调用同一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))];

This is wrong, it calls the ToString() method on the random class and hence it outputs System.Random 这是错误的,它在随机类上调用ToString()方法,因此输出System.Random

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

You need to properly index the values using {0} , {1} for first and second value. 您需要使用{0}{1}作为第一个和第二个值来正确索引这些值。

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

You have to call the Next() method like you did in the other section of your code. 您必须像在代码另一部分中一样调用Next()方法。

That last line of code implicitly calls ToString() on your instance of the Random class, which outputs the class type ( "System.Random" in this case). 最后一行代码在您的Random类实例上隐式调用ToString() ,该实例输出类类型(本例中为"System.Random" )。

尝试这个

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

Once the values are used they are disposed of unless you save them. 一旦使用这些值,除非您保存它们,否则它们将被丢弃。

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 ();
}

Also you only need one random object. 同样,您只需要一个随机对象。

since you're retrieving values in a loop you should probably look at a class for the individual values and a List to store a collection of those values. 由于您是在循环中检索值,因此您应该查看单个值的类和用于存储这些值集合的List。

Update: 更新:

Here's an example of using a class and a 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);
}

each trap coordinate is in the list traps (eg coordinate 1 is traps[0] ) 每个陷阱坐标都在列表traps (例如,坐标1是traps[0]

Have you try this code : 您是否尝试过以下代码:

 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