0

I want to make 2 different Random objects in 2 consecutive lines of code. The parameterless constructer of the Random class is like this:

public Random() 
    : this(Environment.TickCount) {
  }

It uses Environment.TickCount as the seed. TickCount represents the amount of time that has passed since the OS is switched on, right?

I tried the following:

Random r1 = new Random ();
Random r2 = new Random ();

And I found out that the 2 Random objects had the same seed because their Next methods return the same number each time. I was surprised by how fast a line of code can be executed. Then I tried:

long tick1 = Environment.TickCount;
for (int i = 0 ; i < 100000 ; i++) {

}
long tick2 = Environment.TickCount;
Console.WriteLine (tick2 - tick1);

And I get 0. So I iterated 100000 times and still, not even 1 millisecond has passed?

I just want to ask how can I create 2 different Random objects or is there another way to generate random numbers?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • use: Random(SeedValue); Example: Random r2 = new Random(5); – Dane Bouchie Jul 26 '15 at 04:20
  • 4
    Why do you need 2 randoms? – artm Jul 26 '15 at 04:20
  • I want to 2 timers to tick at random intervals @artm – Sweeper Jul 26 '15 at 04:22
  • Then the random would be the same every time I run it but I want the seed to be random every time I run it @DaneBouchie – Sweeper Jul 26 '15 at 04:23
  • Use `Thread.Sleep(someValue);` instead of your _million loop_ .. [like MSDN did here](https://msdn.microsoft.com/en-us/library/ctssatww(v=vs.110).aspx) – Ahmad Ibrahim Jul 26 '15 at 04:27
  • 2
    Don't use `Thread.Sleep(...)`. That's a bad idea. – Enigmativity Jul 26 '15 at 04:32
  • @Enigmativity I know, but if a 100000-times loop won't hurt, then Thread.Sleep won't hurt too :-D – Ahmad Ibrahim Jul 26 '15 at 04:51
  • @AhmadIbrahim - Who said a 100,000 times loop wouldn't hurt? Both are bad. There are much better ways to get a good result rather than resort to either of those methods. – Enigmativity Jul 26 '15 at 05:04
  • @Enigmativity I know both of them are bad, but I mean as long as the OP expected the loop to do him the trick, then he is probably trying to learn something about Random objects rather than he's writing production-ready code. So, I gave him a quick fix to see if that is enough for him. – Ahmad Ibrahim Jul 26 '15 at 05:25
  • _I want to 2 timers to tick at random intervals_ So? One random will do that just as well as two.. – TaW Jul 26 '15 at 08:04

2 Answers2

2

To me, it seems like an XY problem, because you don't need two separate Random instances - you can use the same one to generate all your random number, can't you? Just call Next again and that's it:

var rnd = new Random();
int firstRandomInt = rnd.Next();
int secondRandomInt = rnd.Next();

However, you really need 2 Random instances, you can use the first one to seed the second one:

var rnd = new Random();
var rnd2 = new Random(rnd.Next());
Community
  • 1
  • 1
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • I have a class that holds a `Random` object and in the constructer I write `this.random = new Random()` and I am calling the constructer for a lot of times – Sweeper Jul 26 '15 at 04:26
  • 1
    @Sweeper - then most probably you are doing something wrong, constructor are supposed to get called only once. – Parimal Raj Jul 26 '15 at 04:28
  • 1
    @Sweeper - use a `static` field to hold your `Random` - then you only create one instance. Use a `lock` if you are multi-threading. – Enigmativity Jul 26 '15 at 04:30
  • 1
    @PaRiMaLRaJ No, I am just creating multiple objects – Sweeper Jul 26 '15 at 04:31
  • ok I see. Thanks guys! Why don't you post the answers in the answers section instead of the comment section so that I can accept them? @Enigmativity – Sweeper Jul 26 '15 at 04:32
2

Base on @PankajMishra's answer, try this one:

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

lock block is effective when you use it in a multi-threading program, if you sure just one thread use it, so you can prevent lock to increase your code performance.

Community
  • 1
  • 1
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98