I was hoping there's a built in method in Random for this, but there isn't? So I came up with this function to return the nth instance of a random seed:
public class Program {
static int seed = 123; // This could be anything
static int NextN(int n, int range) // Get the nth instance of seed
{
Random rand = new Random(seed);
List<int> pool = new List<int>();
while( pool.Count< n+1 ){ pool.Add(rand.Next(range)); }
return pool[n];
}
public static void Main()
{
Console.WriteLine(NextN(8,100)); // Return 19 as the 8th value of seed 123 at 100 range.
}}
As you can see, the NextN() function generate the sequence of random numbers first, then return the value of the nth instance. This will perform badly once you're trying to reach a very high instance (like in the millions). Since random always return the same value each time you restart an app, I need to keep track how many times the random seed is accessed between session. Is there a way around this?
Related
I have a SQL column with datatype as Bigint, so when I 'm generating a unique number using C# Random class in a multithreaded environment, I see there are duplicate values instead of having unique, I can see only system GUID is the only option to generate unique Id's, could you please help me to solve this problem.
private Object thisLock = new Object();
public Random a = new Random(DateTime.Now.Ticks.GetHashCode());
private void NewNumber()
{
lock (thisLock)
{
MyNumber = a.Next(0, 10);
}
}
The Random class generates random values, not unique values. In your sample code the maximum number of unique integers is 10 (from 0 to 9). So if you called this method at least 11 times, you would be guaranteed one or more duplicates.
For a database you should use identity columns.
Your code should normally run without error, and I did not find an error. The duplicate numbers may be due to their small range.
You can use the following class to generate random numbers between ranges. Returns a number from the desired range each time.
class UniqueRandom
{
private readonly List<int> _currentList;
private readonly Random _random = new Random();
public UniqueRandom(IEnumerable<int> seed)
{
_currentList = new List<int>(seed);
}
public int Next()
{
if (_currentList.Count == 0)
{
throw new ApplicationException("No more numbers");
}
int i = _random.Next(_currentList.Count);
int result = _currentList[i];
_currentList.RemoveAt(i);
return result;
}
}
create instance from UniqueRandom class and call Next() method in NewNumber() method
UniqueRandom u = new UniqueRandom(Enumerable.Range(0, 10));
private Object thisLock = new Object();
public Random a = new Random(DateTime.Now.Ticks.GetHashCode());
private void NewNumber()
{
lock (thisLock)
{
MyNumber = u.Next();
}
}
The SQL Server Bigint type is equivalent to the C# Int64 (long) type. To generate random long values in C#, look at this question. To ensure that the random number is also unique, add a unique constraint to the relevant datatable column. In case the random value already exists in the database, catch the constraint-violation exception, and try again with a new random value.
Regarding how to use the Random class correctly in a multithreaded environment, check out this question: Is C# Random Number Generator thread safe?
I was searching on google for RNGCryptoServiceProvider with examples on how to limit the range between Max and Min, and still get an even distribution. Before I used modulo operator, but sometimes it I get strange values (above Max)...
Anyways this code (credit to unknown) seeds Random with a new seed from RNGCCryptoServiceProvider, everytime the method is called. What do you guys think?
public static int GetRandom(int min, int max)
{
byte[] b = new byte[sizeof(int)];
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
int i = BitConverter.ToInt32(b, 0);
Random r = new Random(i);
return r.Next(min, max);
}
There is no point in using an encryption class random generator to seed a regular random generator. (By the principle of the weakest link...) Just use a single instance of the random generator, and reuse it:
private static Random rnd = new Random();
public static int GetRandom(int min, int max) {
return rnd.Next(min, max);
}
You'll want to create your RNGCryptoServiceProvider object once and then re-use that object every time you want a new random number. For instance, you can either pass said object into your GetRandom() method or store it in a class-level field.
As far as the RNGCryptoServiceProvider type itself, it generates good numbers on its own, there's no need to create a Random object and pass in a seed. This should give you a very decent distribution:
public static int GetRandom(RNGCryptoServiceProvider rngProvider, int min, int max)
{
byte[] b = new byte[sizeof(UInt32)];
rngProvider.GetBytes(b);
double d = BitConverter.ToUInt32(b, 0) / (double)UInt32.MaxValue;
return min + (int)((max - min) * d);
}
It's a better practice to seed your random number generator just one time in your application.
I suggest you create a static class for random number generation. The random generation object can yield even distribution just by normal usage. I don't know whats the benefit of seeding the generator with RNGCryptoServiceProvider. I prefer using the time as seeding method as usual. Therefore the following code is my suggestion:
int randomNumber=Rand.get(min,max);
public static class Rand
{
private static Random rnd;
static rand()
{
rand=new Random(DateTime.Now.Ticks);
}
public static int get(int min,int max)
{
return rnd.Next(min,max);
}
}
I have two classes, a Teacher class and a Student class. In my program I create an array of 10 Teacher objects and within each Teacher object is an array of 10 Student Objects. Each Student object also has an array of integers as a member variabe, and when each Student is instantiated, it's own array of integers are filled with numbers from a random number generator. My program goes something like this:
An array of type Teacher is created with size 10
The array is then filled with 10 actual teacher objects
Each Teacher object contains an array of Student objects of size 10 as a member variable
The Student array in each Teacher object is filled with actual Student objects
Each student object has an array of integers that are filled with random numbers in the Student objects constructor.
Here is the problem I ran into: It seems that every time the 10 Student objects are created for each Teacher, the random number generator within the Student object constructor does not reset or change even when I call the .Next() function until the next set of 10 Student objects are created for the next Teacher object. What I want is the 10 Teacher objects to each have their own Student Objects which have their own integer arrays filled with randomly generated numbers.
Any help would be appreciated! I ran into this sort of problem with constructors with my last question, and that was a matter of whether something was to be static or not and I'm not sure that's the case this time. Please ask me questions if I wasn't clear enough about anything!!
UPDATE** So after looking at MSDN, I found in their sample code "Thread.Sleep(2000)" and stuck it into my Student Constructor just to see what it did. It seemed to have solved the problem although my program runs a lot slower now, is there a minimum sleep value to wait until the Random.Next() uses a new seed from the clock and even if I did solve the problem, is there a better way to do this? My random number generator is already a static member variable of Student.
If you initialize several Random instances using the default constructor, Random(), within a short time period, then you risk that they would end up with the same seed value (which is time-dependant) and, therefore, generate the same random sequence each time.
You can fix this issue by initializing a single static Random instance, and share it among all Student instances. This is perfectly safe as long as you’re not multi-threading.
public class Student
{
private static readonly Random random = new Random();
}
From the MSDN page on the Random() 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. This problem can be avoided by using a single Random object to generate all random numbers. You can also work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random(Int32) constructor. For more information, see the Random(Int32) constructor.
Edit: Although you state that your random number generator is already a static member, this is still not enough if you’re instantiating it repeatedly (redundantly) during each Student initialization. The following code is still incorrect:
public class Student
{
private static Random random;
private int[] numbers;
public Student()
{
random = new Random();
numbers = new int[10];
for (int i = 0; i < 10; ++i)
numbers[i] = random.Next();
}
}
You need to replace it with a version that only instantiates Random once, like so:
public class Student
{
private static readonly Random random = new Random();
private int[] numbers;
public Student()
{
numbers = new int[10];
for (int i = 0; i < 10; ++i)
numbers[i] = random.Next();
}
}
A common pitfall with Random is to create a new instance each time you need a random value.
for (int i = 0; i < 1000; i++) {
var random = new Random();
list.Add(random.Next());
}
This does not work as expected, since Random uses the actual time to create the first random value. However, the time clock has a limited resolution. It can be that several succeeding iterations return the same time and thus the same (pseudo) random value!
The solution is to create random only once.
var random = new Random();
for (int i = 0; i < 1000; i++) {
list.Add(random.Next());
}
You could also declare random as a static class member
public static readonly Random RandomGenerator = new Random();
A random number generator uses the system time as a seed. I wonder if you are possibly declaring a new generator and calling it before the system has time to move on to its next increment. The processor cycles are actually more than often faster than the system clock. Can you post a code example?
I would suggest declaring a Static generator as either a class property or somewhere else accessible, instantiate it as final. This will guarantee that you wont be overwriting the generator with another.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
c# - getting the same random number repeatedly
Random number generator not working the way I had planned (C#)
I have a method that builds a queue of ints:
public Queue<int> generateTrainingInts(int count = 60)
{
Queue<int> retval = new Queue<int>();
for (int i = 0; i < count; i++)
{
retval.Enqueue(JE_Rand.rInt(2001, 100));
}
return retval;
}
JE_Rand.rInt() is just a function that delegates to a function of the Random class:
public static int rInt(int exclUB, int incLB = 0)
{
Random rand = new Random(DateTime.Now.Millisecond);
int t = rand.Next(incLB, exclUB);
rand = null;
return t;
}
But when I call generateTrainingInts, the same number is enqueued each time. However, if I change rInt to use a static instance of the Random class, instead of a local instance (with function scope as it is defined above), then it appears to work correctly (enqueue random integers). Does anybody know why this happens?
Edit:
Dear Answerers who didn't read my question thoroughly,
Like some of you pointed out, I am looking for a good explanation of why this happens. I am not looking for a solution to the same-number-generated problem, because I already fixed that like I said above. Thanks for your enthusiasm though :) I really just want to understand things like this, because my first implementation made more sense conceptually to me.
You need to keep the same Random object. Put it outside your static method as a static member
private static Random rand = new Random();
public static int rInt(int exclUB, int incLB = 0)
{
int t = rand.Next(incLB, exclUB);
return t;
}
Edit
The reason is the finite resolution of the clock used to initialize Random. Subsequent initializations of Random will get the same starting position in the random sequence. When reusing the same Random the next value in the random sequence is always generated.
Try out the following code and I think you'll see why:
void PrintNowAHundredTimes()
{
for (int i = 0; i < 100; ++i)
{
Console.WriteLine(DateTime.Now);
}
}
The Random objects are getting the same seed over and over. This is because the granularity of the system time returned by DateTime.Now is, quite simply, finite. On my machine for example the value only changes every ~15 ms. So consecutive calls within that time period return the same time.
And as I suspect you already know, two Random objects initialized with the same seed value will generate identical random sequences. (That's why it's called pseudorandom, technically.)
You should also be aware that even if it made sense to instantiate a new Random object locally within your method, setting it to null would still serve no purpose (once the method exits there will be no more references to the object anyway, so it will be garbage collected regardless).
public class JE_Rand
{
private static Random rand= new Random(DateTime.Now.Millisecond);
public static int rInt(int exclUB, int incLB = 0)
{
int t = rand.Next(incLB, exclUB);
return t;
}
}
I am using .NET to create an artificial life program and I am using C#'s pseudo random class defined in a Singleton. The idea is that if I use the same random number generator throughout the application, I could merely save the seed and then reload from the seed to recompute a certain interesting run.
public sealed class RandomNumberGenerator : Random
{
private static readonly RandomNumberGenerator instance = new RandomNumberGenerator();
RandomNumberGenerator()
{
}
public static RandomNumberGenerator Instance
{
get
{
return instance;
}
}
}
I also wanted a method that could give me two different random numbers.
public static Tuple<int, int> TwoDifferentRandomNumbers(this Random rnd, int minValue, int maxValue)
{
if (minValue >= maxValue)
throw new ArgumentOutOfRangeException("maxValue", "maxValue must be greater than minValue");
if (minValue + 1 == maxValue)
return Tuple.Create<int, int>(minValue, maxValue);
int rnd1 = rnd.Next(minValue, maxValue);
int rnd2 = rnd.Next(minValue, maxValue);
while (rnd1 == rnd2)
{
rnd2 = rnd.Next(minValue, maxValue);
}
return Tuple.Create<int, int>(rnd1, rnd2);
}
The problem is that sometimes rnd.Next(minValue,maxValuealways returns minValue. If I breakpoint at this point and try creating a double and setting it to rnd.NextDouble(), it returns 0.0. Anyone know why this is happening?
I know that it is a pseudo random number generator, but frankly, I hadn't expected it to lock at 0. The random number generator is being accessed from multiple threads... could this be the source of the problem?
EDIT : Thanks, the problem ended up being thread safety.
This is the new version of the class.
public sealed class RandomNumberGenerator : Random
{
private static Random _global = new Random();
[ThreadStatic]
private static Random _localInstance;
RandomNumberGenerator()
{
}
public static Random Instance
{
get
{
Random inst = _localInstance;
if (inst == null)
{
int seed;
lock (_global) seed = _global.Next();
_localInstance = inst = new Random(seed);
}
return _localInstance;
}
}
}
The Random class is not thread-safe.
You should make your static instance [ThreadStatic], or protect it with a lock.
If you use only One RNG for multiple threads even if you save the seed, you won't be able to generate the same numbers the next time you launch your application because you won't be sure that the calls to the RNG from the different threads will be in the same order.
If you have a fixed/known number of thread make a RNG per Thread and save each seed.
Forget what I just said if you're 100% sure that each thread will call the RNG with exactly the same order as the last time if you use the same seed.
The idea is that if I use the same random number generator throughout the application, I could merely save the seed and then reload from the seed to recompute a certain interesting run.
You don't actually need a singleton instance of the RNG for this. If you initialize two separate instances of Random to the same seed, they will produce exactly the same sequence.
My advice is, save the seed, but get rid of the singleton.
I don't even have to look up the Random class th know "all instance methods of this class or not thread-safe". That goes for all .NET classes, with a very few exceptions.
So yes, it is the multi-threading. But you haven't mentioned verifying that MaxValue > MinValue either.