I have the following function:
//Function to get random number
public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
How I call it:
byte[] mac = new byte[6];
for (int x = 0; x < 6; ++x)
mac[x] = (byte)(Misc.RandomNumber((int)0xFFFF, (int)0xFFFFFF) % 256);
If I step that loop with the debugger during runtime I get different values (which is what I want). However, if I put a breakpoint two lines below that code, all members of the "mac" array have equal value.
Why does that happen?
Every time you execute
It does not matter if you execute it millions of times, you will always use the same seed.
If you use
You get different random number sequence, if a hacker guesses the seed and your algorithm is related to the security of your system - your algorithm is broken. I you execute mult. In this constructor the seed is specified by the system clock and if several instances are created in a very short period of time (milliseconds) it is possible that they may have the same seed.
If you need safe random numbers you must use the class
Usage:
just declare the Random class variable like this:
if you want to get different random number each time from your list then use
Each time by declaring
Random r = new Random()
once.There are a lot of solutions, here one: if you want only number erase the letters and the method receives a random and the result length.