Why Does Random.Next() Always Return the Same Number

Why does Random.Next() always return the same number

You're always seeding a new instance with the same seed, and then grabbing the first max. By using a Seed, you're guaranteeing the same results.

If you want to have a static, random number generation that does different results, you should rework this a bit. However, since Random is not threadsafe, it requires some synchronization when used statically. Something like:

private static Random random;
private static object syncObj = new object();
private static void InitRandomNumber(int seed)
{
random = new Random(seed);
}
private static int GenerateRandomNumber(int max)
{
lock(syncObj)
{
if (random == null)
random = new Random(); // Or exception...
return random.Next(max);
}
}

Random.Next returns always the same values

The problem is that you are creating instances of the Random class too close in time.

When you create a Random object, it's seeded with a value from the system clock. If you create Random instances too close in time, they will all be seeded with the same random sequence.

Create a single Random object and pass its reference to the constructor when you create instances of the "a" class, instead of creating one Random object for each "a" instance.

Random.Next returns always the same values

You have three problems with your code.

  1. You should externalize your random variable.
  2. You have a problem with truncation error.
  3. The range between min and max is way to large.

The first problem is because you may not have enough time to advance the seed when reinitializing your random variable. The second error comes from truncating your (what would b very large) numbers down to ints. Finally, your biggest problem is your range between your min and your max. Consider finding the range between min and max (as defined in your code) with inputs 1->20:

length  max-min        
1 8
2 89
3 899
4 8999
5 89999
6 899999
7 8999999
8 89999999
9 899999999
10 8,999,999,999
11 89999999999
12 899999999999
13 8999999999999
14 89999999999999
15 899999999999999
16 9E+15
17 9E+16
18 9E+17
19 9E+18

And keep in mind that the maximum integer is 2,147,483,647, which is passed on any number greater than 9.

System.Random().Next() returning the same result

It's best explained by the manual of System.Random()'s default constructor;

The default seed value is derived from the system clock and has finite
resolution. As a result, different Random objects that are created in
close succession by a call to the default constructor will have
identical default seed values and, therefore, will produce identical
sets of random numbers.

Random.Next() sometimes returns same number in separate threads

Random is not thread-safe - you shouldn't be using the same instance from multiple threads. It can get much worse than just returning the same data - by using it from multiple threads, you can get it "stuck" in a state where it will always return 0, IIRC.

Obviously you don't just want to create a new instance for each thread at roughly the same time, as they'll end up with the same seeds...

I have an article which goes into the details of this and provides an implementation which lazily instantiates one instance of Random per thread using an incrementing seed.

C# random function returning the same value

When you create a Random instance it's seeded with the current time. So if you create several of them at the same time they'll generate the same random number sequence. You need to create a single instance of Random and use that.

Will System.Random.Next() always return the same sequence

If I provide a specific seed, will the sequence of random numbers be the same on every computer on which the code is run?

Yes. That is the point of a seed.

Will it continue to be the same with future releases (i.e. is it built into the spec?)

Most likely, but this isn't guaranteed. The documentation for System.Random states:

The current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.

The wording there does leave it open for a future implementation to change algorithms.

Java random always returns the same number when I set the seed?

You need to share the Random() instance across the whole class:

public class Numbers {
Random randnum;

public Numbers() {
randnum = new Random();
randnum.setSeed(123456789);
}

public int random(int i){
return randnum.nextInt(i);
}
}


Related Topics



Leave a reply



Submit