Answered by:
Random dices have the same value ??
Question

I have made a generel generator for random numbers in included intervals:
private int trækEtTal(int fraOgMed, int tilOgMed) { // giver et tilfældigt HELTAL fra og med fraOgMed til og med tilOgMed Random t = new Random((int)DateTime.Now.Ticks); int etTal= t.Next(fraOgMed, tilOgMed+1); return etTal; }
I've used it for a long time without problems but now I'm want to through two dices and calls it in 2 following lines and my generator gives me THE EXACT SAME values and distribution !
OK, I can call it with some delay between the lines, but that's not a good solution.
How can I change my generator so no matter how fast I call it it will allways give me different value and distribution ?
Answers

You just need to seed the generator before the function is called. Don't reseed the generator every time the function is called.
The answer given in the thread linked above applies a lock, in case more than one thread is using the generator. Assuming only one thread, that is not necessary. I would simplify the code to:
//Function to get a random number private static readonly Random random = new Random(); public static int RandomNumber(int min, int max) { return random.Next(min, max); }
Each time RandomNumber is called, you get the next value from the generator (scaled between min and max).Richard Mueller  MVP Enterprise Mobility (Identity and Access)
 Marked as answer by Ksor2 Saturday, December 16, 2017 11:43 PM
All replies

See threads like Random number generator only generating one random number

In brief, the first time you call the generator, use the system clock (DateTime.Now.Ticks) to seed the generator. But on subsequent calls just use the Next function to retrieve the next value in the random sequence. The function is not random if you seed it each time it is called.
Richard Mueller  MVP Enterprise Mobility (Identity and Access)

In brief, the first time you call the generator, use the system clock (DateTime.Now.Ticks) to seed the generator. But on subsequent calls just use the Next function to retrieve the next value in the random sequence. The function is not random if you seed it each time it is called.
Richard Mueller  MVP Enterprise Mobility (Identity and Access)
Exactly this. You should create the Random instance only once and reuse it.

In brief, the first time you call the generator, use the system clock (DateTime.Now.Ticks) to seed the generator. But on subsequent calls just use the Next function to retrieve the next value in the random sequence. The function is not random if you seed it each time it is called.
Richard Mueller  MVP Enterprise Mobility (Identity and Access)
I have red the links in this thread but I dont get it  how can such simple task be made so sophisticated.
I just need a random number generator class added to my project that I can call something like:
aRandomNumber = drawRandomNumber(lowerIncluded, upperIncluded)
what's i the links is way out of my space ;((

You just need to seed the generator before the function is called. Don't reseed the generator every time the function is called.
The answer given in the thread linked above applies a lock, in case more than one thread is using the generator. Assuming only one thread, that is not necessary. I would simplify the code to:
//Function to get a random number private static readonly Random random = new Random(); public static int RandomNumber(int min, int max) { return random.Next(min, max); }
Each time RandomNumber is called, you get the next value from the generator (scaled between min and max).Richard Mueller  MVP Enterprise Mobility (Identity and Access)
 Marked as answer by Ksor2 Saturday, December 16, 2017 11:43 PM