简体   繁体   中英

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. 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".

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. Eg. RNG values for x are (4,7,1) and the y values are (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). 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

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:

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:

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

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

You need to properly index the values using {0} , {1} for first and second value.

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.

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).

尝试这个

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.

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] )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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