I'm creating a program which creates several panels with randomly determined colors which will be incremented/decremented by some random constant every 20 milliseconds or so to create a smooth, undulating color. For this, I've just been using the Next(int) method of the Random class. For a single instance, this works perfectly--every time I run the program, I get different colors changing at different rates. The problem comes when I try to create multiple panels--most, if not all, come out looking and behaving identically to each other, implying that all of the randomly generated numbers were identical. I'm assuming that this is a result of generating all of the pseudorandom numbers in rapid succession, causing all of them to be based on the same seed.
Is there a better way than using the Random class to generate random integers in rapid succession to ensure that they're not identical? If there isn't any way already built into C#, is there any straightforward way to develop a pseudorandom number generator (bearing in mind that this is my first foray into using C#)?
Do not use multiple instances of the Random Class using the default constructor. If they all are initialized within the same time slice they will all have the same seed and will all generate the same sequence of random numbers, use the constructor that you can pass in a seed and pass a different seed to each instance.
Random rand0, rand1, rand2;
void init()
{
int baseSeed = (int) DateTime.Now.Ticks;
rand0 = new Random(baseSeed);
rand1 = new Random(baseSeed + 1);
rand2 = new Random(baseSeed + 2);
}
Also you only need one object per thread, use the same Random object for all of the panels if they are all on the same thread.
Random is fine if you don't need cryptographically secure random numbers - but chances are you're creating a new instance every time you need a number, rather than using one instance throughout.
When you create a new instance of Random it will take "the current time" as the seed - so if you create two in quick succession, you'll end up with two instances with the same seed and therefore the same numbers (when you use the same calls).
It's generally better to use a single instance of Random - or rather, one per thread, as it's not thread-safe.
See my article on the topic for various approaches.
If you have multiple instances of the Random class that are producing identical series of random numbers, then you must be creating each instance using the same seed. For example:
The most likely explanation for that is that you are creating them all like this:
var rng = new Random();
and creating each instance at the same point in time. This constructor uses the current time to seed the RNG and if you create them all at the same point in time, they will all be seeded with the same time.
Solve the problem by creating each instance with a different seed. Or create a single RNG and share it between all panels.
I prefer RNGCryptoServiceProvider. It is roughly as fast as Random and tends to produce more unique values in my informal testing. You also avoid any undesirable behaviors pertaining to seeding (such as others are describing).
However, you can't guarantee uniqueness (otherwise it wouldn't be random). You can use a database if you need to permanently track unique values (which it doesn't sound like you wish to do) or a Dictionary where the random value is the key if you just care about generating a set of unique numbers in memory. If the key already exists, you reject the value and generate another one.
using System.Security.Cryptography;
...
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
byte[] bytes = new byte[8];
crypto.GetBytes( bytes );
long value = BitConverter.ToInt64( bytes, 0 );
As mentioned above, you can use a single instance of the random class. However, if for some reason this isn't desirable (i.e. you want to run multiple threads and don't want any contention), then you can use multiple instances as long as you initialize the seed for each generator to a random or pseudo-randomly generated seed.
For this initial seed generation, you will need to use the same Random generator.
Related
I am using this code to pick a random element from a list:
var rand = new Random();
var i = rand.Next(words.Count);
keyword = words[i].keyword;
Is this the optimal way to do this or is there a better way that I could employ? What I am particularly concerned about is will this be completely random?
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.
Every time you do new Random() it is initialized . This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.
Hope this helps!!!
It will be absymally un-random, you must not create a new Random instance each time you need a number. Doing that completely ruins the statistical properties of the generator.
Even then you will not achieve perfect randomness (you need external hardware for that), but it ought to satisfy all the principal properties of randomness.
Put this line for single time initialization: var rand = new Random();
Use below code to generate new number each time.
var i = rand.Next(words.Count);
keyword = words[i].keyword;
Hope it will help you.
I tried to understand better random number generating process, in particular pseudorandom number generation in .NET (actually MONO), and I am after reading these articles: MSDN Random, wikipedia Random number generation.
So I think I get it (.NET uses modified version of Donald E. Knuth's subtractive random number generator algorithm).
The only thing that stays unclear to me is how this algorithm gets its first seed number. I tried to find info, but didn't.
Does it takes from pseudorandom number generator too? It doesnt make sense, does it? First seed needs to be somehow random too, couse if not first random number in every instance of .NET application would be the same. If so, quessing next results from applications like games would be quite easy (if the author of application didn't modify the number after generation). So I am quessing the first seed number, I mean first input to the generator must be taken from something unique for machine or current application process, but I am just quessing.
So my question is: If my thinking process is correct (if not please correct me), how generator gets its first input for given algorithm?
From the Reference Random Class
public Random()
: this(Environment.TickCount) {
}
public Random(int Seed) {
///....
}
It's calculated based on your input or the Tick count
The default seed value is derived from the system clock
source Random() constructor
So separate instances get different random values. However if you create multiple instances of this class quickly, they may end up using the same clock-tick as seed - and thus generate the same sequence.
The Random class has a constructor that takes either no or one parameter of type int. Thus you can set the initial seed by your own.
If you do not provide a parameter than as the documentations says a time-dependent value is used as seed. The default values is the number of ticks (see source) that have elapsed since the system started up.
System.Random uses Environment.TickCount as the seed in its parameterless constructor, as per .NET source.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why does it appear that my random number generator isn't random in C#?
How can I generate truly (not pseudo) random numbers with C#?
I've created a dice game where the dice is based on a percentile, 1-100.
public static void Roll()
{
Random rand = new Random((int)DateTime.Now.Ticks);
return rand.Next(1, 100);
}
But I don't feel like it's a real random based on current time.
If I do
for (int i = 0; i < 5; i++)
{
Console.WriteLine("#" + i + " " + Roll());
}
They would all be the same values, because the DateTime.Now.Ticks didn't change, it seeded the same number.
I was thinking I could generate a new random seed if the seed was the same due to the current time, but it doesn't feel like an honest "re-roll"
What should I do to try and replicate a close to real/honest dice roll? Should I use the RNGCryptoServiceProvider class to generate rolls instead?
DateTime.Now.Ticks only has a resolution of approximately 16ms, so if you create a Random with that overload multiple times within a 16ms "slot" they will all be seeded with the same value and therefore you will get the same sequence.
Initialize your Random outside your loop so that a single Random sequence is produced, rather than creating it each time within the loop which could result in Randoms being seeded with the same value and so produce the same sequence.
Update
My previous point that the default constructor initialized Random with CPU ticks was incorrect, the default constructor actually uses Environment.TickCount which is:
A 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started.
Which still has a low resolution. If you make multiple instances of Random in quick succession, they can easily be created within the same time slot and therefore have the same seed value, and create the same sequence. Create a single instance of Random and use that.
Update
Further to your comments, if you wish to generate a random sequence across multiple threads, please see the following Jon Skeet article which discusses a thread-safe wrapper:
https://codeblog.jonskeet.uk/2009/11/04/revisiting-randomness
Pseudo-random number generators like Random should only be seeded once, so:
private static Random _rand = new Random();
public static int Roll()
{
return _rand.Next(1, 100);
}
(Note I made the return value int rather than void; the Roll function as quoted in the question results in a syntax error.)
But your title says "Creating a true random". Random won't do that for you, it's a pseudo-random number generator, meaning it's deterministic, just hard to predict if you don't know the seed. Usually that's good enough for most purposes, but if you need real randomness, you need an entropy source. http://random.org is one popular one.
You should create your Random class only once outside your Roll function and seed it with a unique value.
You are recreating your Random each time you call Roll which causes the 'not random numbers'.
Should I use the RNGCryptoServiceProvider class to generate rolls instead?
If this is a serious game with money at stake then: Yes.
I'm assuming that you are calling the Roll() method so quickly that Now.Ticks is the same?
The simplest way to get around this would be rather than to create a new Random() instance each time you call Roll() create a static variable to hold a single instance of Random().
The usual way to use random number generators is to seed them once, save them and call on them repeatedly throughout your programme. As long as you seed from a suitable value at the start, you should get acceptable randomness - assuming the generator you're using is using a function returning things which are suitably random for your purposes. So, save your Random instance outside the Roll() function, seed it the first time it's used, then just call Next() on it each time you need another number.
When you get right down to it, there's no such thing as true random number generation on a computer, only pseudorandom sequences based on a seed. However, humans are terrible at identifying randomness, so it's usually okay.
In my game I'm going to use random values to pick the reward the player gets from a chest. The problem is that you can quick save and quick load and that means they can keep reloading to re-randomize until they get what they want. Is there some way that I could get the current seed value of my Random object and possibly return to that same point when they load so that they couldn't abuse the randomization?
This is not possible.
Instead, you can serialize the Random instance using binary serialization.
Random is [Serializable], and the seed and internal state will persist.
Note, however, that saving the random seed allows your players to predict the future, which is very useful if you allow saving in battle.
Also note that users can still save, open the chest, load, perform an action that generates a random number, then get a different item from the chest.
Not sure on getting the seed, but you could save the value you give to the Random object. Remember, there are two constructors. The second is Random(Int32), so if you set the seed yourself (an easy enough value is Environment.TickCount), you could store that value somewhere before you pass it to the constructor. If you haven't read it yet, check out the MSDN documentation at https://learn.microsoft.com/en-us/dotnet/api/system.random.
Indeed, the Seed isn't stored as it is not relevant for the algorithm after initialization. One of its derivatives, mj, is stored in the SeedArray though, you can check that using Reflection to compare both Random instances:
int subtraction = (Seed == Int32.MinValue) ? Int32.MaxValue : Math.Abs(Seed);
mj = MSEED - subtraction;
SeedArray[55]=mj;
So all you have to do is to check the last element (index 55) in the SeedArray. This is the only place Seed is used.
[Moved answer from deleted question How to determine if two Random instances have the same seed?]
You can calculate the random reward as a hash function of:
some seed that is assigned when you begin a new game, and is persisted in saved games; and
some constant property of a chest that is invariant across all games (e.g. a fixed ID, or its position if it never moves).
This method has the advantage that a given chest will always yield the same reward in a given game, no matter how many times you save and replay, even if chests are opened in different orders, or other 'random' events are triggered in different orders. Also each chest's reward is independent of other chests' rewards, so long as the chest's property used in the hash is independent.
In the following example GetRewardId generates a reward ID as a hash of the game seed XORed with the x coordinate of a chest. It uses Random to perform the hash, by using the hash input as the Random object's seed, and taking the first randomly generated number as the output.
private static int GetRewardId(int seed, float coord, int numRewards)
{
int tempSeed = BitConverter.ToInt32(BitConverter.GetBytes(coord), 0) ^ seed;
return new Random(tempSeed).Next(numRewards);
}
int seed = new Random().Next();
int numDifferentRewards = 5;
float xCoordinate = chest.Position.X;
int rewardId = GetRewardId(seed, xCoordinate, numDifferentRewards);
If many of your chests are likely to be aligned in sace, you may want to choose a different property, or use additional dimensions, by XORing with the y and/or z coordinates too.
I'd probably just use this as per MSDN: http://msdn.microsoft.com/en-us/library/ctssatww.aspx
Random(seed)
where seed is some value I've loaded from storage.
This is only related on a tangent, but in case anyone is wondering why Random doesn't have a property called Seed or a method called GetSeed(), I'm willing to wager that it's likely due to security concerns: Would you want to expose the inner workings of your "random" number generator to the outside world? Absolutely not! Otherwise, some client code could poke around until it got the values you were using and then do nasty and unintended things with them.
Unfortunately, in the reference implementation from Microsoft, the no arg ctor's seed value is not even saved, let alone exposed for access:
http://referencesource.microsoft.com/#mscorlib/system/random.cs,bb77e610694e64ca
However, as you can also see in the reference implementation, the value you can pass in (probably should -- I know I do), just like they do, is: Environment.TickCount
So save that to a variable, then pass that variable in to the ctor that takes an arg and you now know the seed. Not after the fact, but this should be sufficient for whatever your intent is.
I recommend you to generate a random number and use it as a seed number to your real random number generator. By this method you have a seed number that is actually a random number and you can save your seed number for further using.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
random string generation - two generated one after another give same results
I am writing a simple dice game for windows phone 7, that involves rolling two dice at the same time. Here is my Dice Roll Code:
private int DiceRoll()
{
int result;
Random rnd = new Random();
result = rnd.Next(1, 7);
return result;
}
I then have this code that rolls the dice when a button is clicked:
private void roll_Click(object sender, RoutedEventArgs e)
{
roll1 = DiceRoll();
roll2 = DiceRoll();}
My problem is that both die get the same result.
Any idea how I can get a rolling algorithm that will usually return different results, but occasionally return the same?
The default seed for Random is based on the current time. To quote the documentation,
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. This problem can be avoided by using a single Random object to generate all random numbers.
That is exactly what you should do: create one instance of Random and use it to generate all your random numbers.
You need to keep one Random object around and reuse it; every time you create a new Random object, you effectively reset the sequence of numbers to begin in the same place. Store the Random object as a member variable someplace. You'll also want to seed it with a different value each time you run the program -- for example, a value based on the system clock time.
The clear majority of 'random number' tools I've seen fail badly if you allocate two or more random objects in a single application. You're allocating a new Random object for every invocation, and each time they are going to be seeded with something pretty weak, and maybe even identical seeds.
So, generate a single Random object and use it over the life of your application.