Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generator only generating one random number

Tags:

c#

random

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?

like image 350
Ivan Prodanov Avatar asked Apr 20 '09 12:04

Ivan Prodanov


People also ask

How do I generate two random numbers in Excel?

To generate multiple random numbers in multiple cells, select the target cells, enter the RANDBETWEEN function, and press control + enter to enter the same formula in all cells at once.

Can math random get 1?

random() The Math. random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.

Can you trick a random number generator?

As you can see, it is completely possible to hack an RNG that's based on a computer program like the ones used in casinos and online games. That's not to say, however, that it is easy. These companies spend a pretty penny to make sure that their games are secure with extensive protocols installed.


2 Answers

For ease of re-use throughout your application a static class may help.

public static class StaticRandom {     private static int seed;      private static ThreadLocal<Random> threadLocal = new ThreadLocal<Random>         (() => new Random(Interlocked.Increment(ref seed)));      static StaticRandom()     {         seed = Environment.TickCount;     }      public static Random Instance { get { return threadLocal.Value; } } } 

You can use then use static random instance with code such as

StaticRandom.Instance.Next(1, 100); 
like image 38
Phil Avatar answered Oct 27 '22 07:10

Phil


Every time you do new Random() it is initialized using the clock. This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.

//Function to get a random number  private static readonly Random random = new Random();  private static readonly object syncLock = new object();  public static int RandomNumber(int min, int max) {     lock(syncLock) { // synchronize         return random.Next(min, max);     } } 

Edit (see comments): why do we need a lock here?

Basically, Next is going to change the internal state of the Random instance. If we do that at the same time from multiple threads, you could argue "we've just made the outcome even more random", but what we are actually doing is potentially breaking the internal implementation, and we could also start getting the same numbers from different threads, which might be a problem - and might not. The guarantee of what happens internally is the bigger issue, though; since Random does not make any guarantees of thread-safety. Thus there are two valid approaches:

  • Synchronize so that we don't access it at the same time from different threads
  • Use different Random instances per thread

Either can be fine; but mutexing a single instance from multiple callers at the same time is just asking for trouble.

The lock achieves the first (and simpler) of these approaches; however, another approach might be:

private static readonly ThreadLocal<Random> appRandom      = new ThreadLocal<Random>(() => new Random()); 

this is then per-thread, so you don't need to synchronize.

like image 138
Marc Gravell Avatar answered Oct 27 '22 05:10

Marc Gravell