Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does every machine generate same result of random number by using the same seed?

Tags:

c#

.net

random

seed

I'm current stuck in the random generator. The requirement specification shows a sample like this:

Random rand = new Random(3412);

The rand result is not directly given out, but used for other performance.

I'd written the same code as above to generate a random number by a seed 3412. however, the result of the rest performance is totally different with sample.

The generating result is 518435373, I used the same code tried on the online c# compiler, but getting different result of generation which is 11688046, the rest performance result was also different with the sample.

So I'm just wondering is that supposed to be different in different machines?

BTW, could anyone provide the result from your machine just see if it's same with me.

like image 777
Ivan Li Avatar asked Aug 31 '12 11:08

Ivan Li


People also ask

Are all random number generators the same?

The random-number-generator principle is the same for all gambling devices. There might be some variation between programs, as there are with all programs, but the randomization process is, for all intents and purposes, the same. As for whether or not the RNG can be manipulated, of course it can.

What would happen if the same seed value is used each time a random object is created?

If the same "seed" value is specified, the sequence of psuedo-random numbers will be the same. This example shows the generation of 2 sets of 8 random integer numbers from 0 to 34 inclusive, where the random seed is set at 55 for both sets.

What is the point of using a seed when generating random number?

A random seed is a starting point in generating random numbers. A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001).

Which seed is used to generate a completely different random number every time you run a program?

The function srand () is used to provide seed for generating random numbers while rand () function generates the next random number in the sequence. => Look For The Entire C++ Training Series Here.


1 Answers

I would expect any one implementation to give the same sequence for the same seed, but there may well be different implementations involved. For example, an "online C# compiler" may well end up using Mono, which I'd expect to have a different implementation to the one in .NET.

I don't know whether the implementations have changed between versions of .NET, but again, that seems entirely possible.

The documentation for the Random(int) constructor states:

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.

... but it doesn't specify the implications of different versions etc. Heck, it doesn't even state whether the x86 and x64 versions will give the same results. I'd expect the same results within any one specific CLR instance (i.e. one process, and not two CLRs running side-by-side, either*.

If you need anything more stable, I'd start off with a specified algorithm - I bet there are implementations of the Mersenne Twister etc available.

like image 141
Jon Skeet Avatar answered Nov 02 '22 16:11

Jon Skeet