Random.Next() Digits always gives the same output every runtime - c#

I know there are lots of questions about random in here but they didn't help me at all, they are about generating same numbers in a single runtime, and mine isn't
So here is my code.
Random rnd = new Random(0);
for (int c = 0; c < arraySize; c++)
data[c] = rnd.Next();
if i run it for the first time.
the element of rnd.Next() would be 1559595546
next output would be 1755192844
and so on,
if i close the program and re run it,
the same set of numbers would be generated
data[0] = 1559595546
data[1] = 1755192844
and so on,
Why is that so? isnt it should generate different set of numbers every time i close the program and run it?
please help me understand this.

No, you're providing a fixed seed of 0. Therefore the output is always the same. Take a read of the documentation for the constructor overload that you are using:
http://msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx
paying particular attention to the following statement:
Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.
Perhaps you should:
var rnd = new Random(); //no seed

Thsi is because you have provide Seed 0 like
Random = new Random(0); //0 is Seed
If you remove it then it will generate different random numbers as you required.

Related

Not jumping into code while not debugging [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 3 years ago.
for (int i = 1; i < 10; i++)
{
number = randomNumber();
if (!straightLine.Contains(number))
{
straightLine.Add(number);
}
}
public static int randomNumber()
{
Random random = new Random();
return random.Next(1, 100);
}
when I debug it works fine but when I run the program it gets 1 random number and that's it. so the problem is that the random Number method is only called once (when I don't debug then it calls it every time)
what can I do?
germi's answer is slightly misleading as it implies that any repeated instantiation of a new Random will produce the same sequence of values. This isn't quite correct, because your debug code works as you expect.
The documentation for Random says that the Random number generator is seeded from the system clock if you dont pass a seed value. https://learn.microsoft.com/en-us/dotnet/api/system.random.-ctor?view=netframework-4.8
The reason it works in debug is that debugging code is very slow (you're literally taking hundreds of milliseconds to step through the code a line at a time) and the clock has time to change in between runs of the loop.
When your code is run at full speed it runs so quickly that the system clock simply won't have changed to a new milliseconds value, so your repeated making of a new Random will result in it being seeded with the same value from the clock
If you were to insert some delay in the code such as Thread.Sleep(1000) in the loop then it would work. If you were to run the loop a million times it would take long enough to work through that the clock would eventually change - a million iterations might take long enough for a small handful of values to come out of the Random
The recommendation for a solution is sound though; make one new Random somewhere outside of the loop and then repeatedly call it. You could also seed the Random with something that is unique each time (like the value of i), though you should bear in mind that providing a particular seed will guarantee that the same random number comes out of it when you call Next. In some contexts this is useful, because you might want a situation where you can provide a certain value and then see the same sequence of random numbers emerge. The main thing to be mindful of is that by default the Random starts it's sequence based on the clock; two different computers with different time settings can theoretically produce the same random numbers if their clocks are reading the same at the moment the Random is created.
Repeatedly instantiating a new Random instance will lead to the same number being generated, since the seed will be the same.
The solution is to have one instance of Random in the class that generates the values:
var random = new Random();
for (int i = 1; i < 10; i++)
{
number = random.Next(1,100);
if (!straightLine.Contains(number))
{
straightLine.Add(number);
}
}
Note that this behavior only exists in .NET Framework, .NET Core will produce different values even across multiple Random instances created in quick succession.

Seeding Multiple Random Number Generators

I have recently been discussing the initialisation of multiple random number generators of the same type in the comments of another post and in that discussion we asked the following questions:
1) Is it a good idea to create multiple instances of the same random number generator with different seeds and use these random number generators in different parts of the program?
2) In particular, can the technique of creating random number generators using the .Net Random class, seeded as below, and using each RNG in different program contexts cause problems:
int size = 64; // The number of RNGs to use
int seed; // Get seed using some normal technique
Random[] r = new Random[size];
for (int i = 0; i < size; i++)
{
r[i] = new Random(seed + i);
}
3) What would you recommend instead if multiple streams of random numbers are required?
4) How would you recommend generating random numbers when thread safety is required?
1) Is it a good idea to create multiple instances of the same random number generator with different seeds and use these random number generators in different parts of the program?
No. The above scheme is in general not recommended.
In his book, The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Addison-Wesley, Reading, MA, third edition, 1997, Dr. Knuth states that
It is not easy to invent a foolproof source of random numbers.
In this case, I point out that taking subsequences from a random sequence may be less random than the original sequence of random numbers:
Random Numbers
PCG
Notice that Micosoft's Random implementation is based on a subractive lagged-fibonacci generator:
Reference Source - System.Random
This kind of random number generator is known for an inbuilt three-point correlation, after all, we're generating the next random number:
These kinds of Random Number Generators also depend heavily on the initialisation of their initial 55 number state. Poor initialisation may lead to poor random numbers. In the above case, similar states, may result in correlated random numbers from each of the different random number generators. Microsoft even recommends against this in their MSDN post about System.Random: MSDN The System.Random class and thread safety:
Instead of instantiating individual Random objects, we recommend that you create a single Random instance to generate all the random numbers needed by your app.
We shall look at an example where a particular initialisation creates strong correlation between the different random number generators and look for alternatives.
2) I have implemented a program that attempts to initialise 64 instances of Random as described above so that we observe any visible flaws. I chose a particular initialisation as a proof of concept:
int size = 64; // The number of random numbers generators
int length = 20; // The number of random numbers from each generator
int steps = 18; // Move 18 steps forward in the beginning to show a particular phenomenon
Random[] r = new Random[size];
for (int i = 0; i < size; i++)
{
r[i] = new Random(i + 1);
// move RNG forward 18 steps
for (int j = 0; j < steps; j++)
{
r[i].Next(3);
}
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < length; j++)
{
Console.Write(r[i].Next(3) + ", "); // Generate a random number, 0 represents a small number, 1 a medium number and 2 a large number
}
Console.WriteLine();
}
This program generates the output shown here, each row represents the output from another RNG:
Notice that the highlighted columns: at particular places the RNGs seem to synchronise and produce output that does not look independent from each other.
I also wish to add another note, that creating a single list of random numbers and taking one random number from the list of each row also produces poor looking random numbers (the RNG being used here is known to have fail some statistical after all!).
3) The type of RNG used depends on your context. Some may be happy with the above output. In other cases, the RNG used may be unusable (Monte Carlo Simulation and Cryptography are two scenarios where System.Random should never be used, even for one stream of random numbers).
If you need to extract multiple subsequences of Random Numbers, find an RNG that has been designed for that purpose:
PCG
4) Finally, what if I want to use System.Random in multiple threads?
Microsoft MSDN has the answer in the same link I referred to above:
MSDN The System.Random class and thread safety
Not sure what "multiple streams of random numbers" means. In random numbers there is no relationship between any two random numbers, there is no order, each is a standalone instance.
If you use a cryptographic PRNG no seeding is necessary. Consider the .net RNGCryptoServiceProvider Class.

C# System.Random needs sleep to work [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 8 years ago.
for (int i = 1; i <= 55; i++ )
{
System.Random myRandom = new System.Random();
//int myInt; 3 lines just alternate method
//myInt = myRandom.Next();
//System.Console.WriteLine(myInt);
System.Console.WriteLine(myRandom.Next());
Thread.Sleep(15); // if less, trouble
}
I kept getting multiples of the same number, up to twenty at a time, instead of each consecutive number being different. For some reason I added the sleep statement and it works if the value is 15 or greater. In fact, if you increment the sleep argument value, you can 'select' how many consecutive repeated numbers you get. Setting a range made no difference. Is the loop faster than the random number generator? This is not serious, I am working through the book "Essential C# 5.0". I looked at least fifty "random" questions, but none covered this phenomena. The three commented lines are a different way to do the same thing, I did not execute both in the same build.
The problem lies in this line:
System.Random myRandom = new System.Random();
It should be outside the for-loop. Why? Because it generates a random seed based on the timing information, and as you create them one after another, they each get the same seed. When you make the thread sleep in-between the iterations, the time changes enough so that the next Random gets a different seed value.
Try this instead:
System.Random myRandom = new System.Random();
for(int i = 1; i <= 55; i++)
{
// Here be the rest of the code
}
Now, the Random object is created just once, getting its seed value just once.

Why Random makes same selection always? [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 9 years ago.
When I executes the following code I'm getting the same color selected always.
static void Main(string[] args)
{
string[] Colors = new string[10] { "Red", "Yellow", "Green", "Blue", "Purple", "White", "violet", "orange", "indigo", "blue" };
for (int i = 0; i < 13; i++)
{
Random rnd = new Random();
int code = rnd.Next(0, 9);
string Color = Colors[code];
Console.WriteLine(Color);
}
Console.ReadLine();
}
But if ` Random rnd = new Random();' is created outside the loop, then the result is unique. If the loop executes in faster rate then the output will be the same. Suppose I do some Database insert operation in the for loop the result will be a different one (a random one)(step by step execution using break points will also result random selection).
Is it really impossible to supply different seeds for such a small duration?
Random uses current time as a seed. When you create it in loop, it happens so fast that time stays the same for every creation. So seed is the same, and values, generated by Random will be the same too.
Try make random object a static member:
private static Random rnd = new Random();
This prevents building several random objects with a same seed (current time), and prevents producing a same sequence of numbers.
Your loop initializes a new instance of Random with the same seed (current time) upon each iteration. Each instance contains a sequence of various random numbers. The code uses the first number from the sequence, and after the iteration is finished the random object is thrown away, and a new Random object is instantiated. Since the code has been run quite fast, the next random object is created at the same time as the previous one, therefore it has the same seed as that one. The new object contains a sequence of various numbers, but the sequence is the same as the previous one (i.e., they have a same first number, second number, and so forth). Again the code uses the first number from the very same sequence, which results in repetitive numbers.
If you make the Random object a static member, the random sequence is created once, and the code will use the next number (not always the first number) of that sequence, hence you will iterate through the sequence of various random numbers.
If you don't want to make the random object a static member, try to feed its constructor a unique seed. You can make use of your loop variable for this purpose.
If you don't provide a seed, Random will use Environment.TickCount for a seed. In a short loop like this, it's entirely possible that the the whole loop is executed in one tick. So the seed is the same every time, hence your "random" number is, too.
Just use the same random object for the entire loop.
Random is not random in computer programming ;) You can make it "more" random by including a seed or by having a static object containing the random :)
you need to keep the same random object for the reason below:
Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes.
The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated.
http://msdn.microsoft.com/en-gb/library/system.random.aspx

Control over random numbers in C#

If I use two random numbers, how can I ensure that the first of these numbers generated is always larger than the second in order to present a subtraction or a divide quiz to the user?
You don't.
Just check which one is larger and present accordingly.
You generate the second random number and add it to the first one.
var max = 1000;
var rnd = new Random();
int a, b;
do
{
a = rnd.Next(max);
b = rnd.Next(max);
} while (a <= b);
You can use similar approach for more complex conditions too (for example if your task is to generate 2 numbers that in sum give more than 100, etc).
You will have to make your code smarter if probability of random numbers satisfying your condition is so small that generation takes too much time but for this particular task this approach is good enough.
You can generate numbers like this (pseudocode): (int)(a*rand()+b) where a and b control the range and starting point of your random numbers.
If a1=10, b1=1 for instance you get a range of 1-10. With a2=10 and b2=11 you get numbers in the range 11-20, which might be good for simple subtraction problems.

Categories