1

I was trying to generate random numbers using "Random" class with Next() and NextDouble() methods.

When I run the program I have different numbers.

But every time I run it I see the same numbers. What to do?

TaW
  • 53,122
  • 8
  • 69
  • 111
EmiTis Yousefi
  • 363
  • 4
  • 9
  • 7
    Show your code! – Salah Akbari Nov 21 '15 at 10:58
  • 2
    Did you read the related column on the right? In particular http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number?rq=1 – Steve Nov 21 '15 at 11:04
  • Both the 'duplicate' and the two answers are __wrong__, imo. You need to make sure to understand your options when creating i.e. when seeding the [Random](https://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx) object. The two constructors give you a choice of creating __repeatable or non-repeatable__ sequences.. If you want non-repeatable sequnces each time you run the program, use the __parameterless constructor__! – TaW Nov 22 '15 at 13:12

1 Answers1

1
Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7);   // creates a number between 1 and 6
int card = rnd.Next(52);     // creates a number between 0 and 51

If you are going to create more than one random number, you should keep the Random instance and reuse it. If you create new instances too close in time, they will produce the same series of random numbers as the random generator is seeded from the system clock.

You can see original post in stackoverflow here. And for more visibility, you should have to post your code in post.

Community
  • 1
  • 1
Bhavik Patel
  • 752
  • 1
  • 5
  • 20