Create a random int number that differ from previous run of program - c#

I use this code to generate a random number.
Random R = new Random(0);
int Rand = R.Next(7);
but i get the same random number in each run of program.

Remove the 0 from the constructor and you'll get different random numbers.
If you pass a number to the constructor it's used as seed, by always specifying 0 you'll always get the same sequence.
You can specify an int32 which is random, but the easiest is to just not pass any parameter and you get a timebased seed

you have to change the seed of your random number generator object everytime you run your program, as what i've seen from you example, your current seed is 0, so you have to change it to something else if you want to get a different stream of random number... just a thought!

Seed your (pseudo)-random generator using a non-constant value, e.g. current time and date:
Random R = new Random(DateTime.Now.Ticks);
Read more about pseudo-random generators at Wikipedia.

Use time as initial seed of your PRNG.

You need to seed the random generator. You can use as follows:
Random R = new Random(DateTime.Now.Millisecond);
int Rand = R.Next(7);

Random number generators generate a new 'random' value based on the previous number generated. The seed is the initial value for this.
Seeding with the same value (like 0 in your example code) basically tells the random number generator to start with the same number each time. Having the exact same random number generated each time means your code becomes restartable. Example: Simulations use this to restart the simulation with changed parameters, but with the same 'data set'.
Another example:
I want to send myself a motivational message each day. Sometimes the messages are garbled. Being able to rerun the script, producing the same message again and again during a day, makes fixing this simple. In Perl code this means:
# By initialising the random generator with the day number since
# the epoch, we get the same quote during one day.
srand(time()/(24*3600));
my $i = int(rand(#messages));
If you want to produce different numbers each time, you will have to set this seed to something random. The options are many, like time, PID, delay between two keystrokes by the user, some value derived from the ethernet interface, etc. or more likely a combination of the above like time*PID.
Hope this clarifies the idea behind the concept of a random number seed value.

if we want a random number between 1 and 100 the code would look like this:
RandomNumberGenerator.GetRandomInt(1, 100)

The most secure way to generate random number is to use the System.Security.Cryptography.RandomNumberGenerator class.
Here is an example that will generate a number between 1 and 100;
public Number Rand()
{
byte[] Salt = new byte[8];
System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(Salt);
decimal result = 0;
foreach (byte b in Salt)
{
result = result * 255 + b;
}
while (result > 100)
{
result /= 10;
}
return result
}

Complete Code:
public static class RandomHelper
{
static object _myLock = new object();
static Random _random = new Random();
public static int RandomNumber(int min, int max)
{
lock (_myLock)
{
if (min == max)
return min;
if (min > max)
return _random.Next(max, min);
return _random.Next(min, max);
}
}

You need to seed the Random class with something more variable than 0. I normally use DataTime.Now.Ticks or you could use a new Guid's integer value.

Related

What exactly does the parameter seed in MLContext do? [duplicate]

This is my code to generate random numbers using a seed as an argument:
double randomGenerator(long seed) {
Random generator = new Random(seed);
double num = generator.nextDouble() * (0.5);
return num;
}
Every time I give a seed and try to generate 100 numbers, they all are the same.
How can I fix this?
If you're giving the same seed, that's normal. That's an important feature allowing tests.
Check this to understand pseudo random generation and seeds:
Pseudorandom number generator
A pseudorandom number generator (PRNG), also known as a deterministic
random bit generator DRBG, is an algorithm for generating a sequence
of numbers that approximates the properties of random numbers. The
sequence is not truly random in that it is completely determined by
a relatively small set of initial values, called the PRNG's state,
which includes a truly random seed.
If you want to have different sequences (the usual case when not tuning or debugging the algorithm), you should call the zero argument constructor which uses the nanoTime to try to get a different seed every time. This Random instance should of course be kept outside of your method.
Your code should probably be like this:
private Random generator = new Random();
double randomGenerator() {
return generator.nextDouble()*0.5;
}
The easy way is to use:
Random rand = new Random(System.currentTimeMillis());
This is the best way to generate Random numbers.
You shouldn't be creating a new Random in method scope. Make it a class member:
public class Foo {
private Random random
public Foo() {
this(System.currentTimeMillis());
}
public Foo(long seed) {
this.random = new Random(seed);
}
public synchronized double getNext() {
return generator.nextDouble();
}
}
This is only an example. I don't think wrapping Random this way adds any value. Put it in a class of yours that is using it.
That's the principle of a Pseudo-RNG. The numbers are not really random. They are generated using a deterministic algorithm, but depending on the seed, the sequence of generated numbers vary. Since you always use the same seed, you always get the same sequence.
Problem is that you seed the random generator again. Every time you seed it the initial state of the random number generator gets reset and the first random number you generate will be the first random number after the initial state
If you'd want to generate multiple numbers using one seed you can do something like this:
public double[] GenerateNumbers(long seed, int amount) {
double[] randomList = new double[amount];
for (int i=0;i<amount;i++) {
Random generator = new Random(seed);
randomList[i] = Math.abs((double) (generator.nextLong() % 0.001) * 10000);
seed--;
}
return randomList;
}
It will display the same list if you use the same seed.
Several of the examples here create a new Random instance, but this is unnecessary. There is also no reason to use synchronized as one solution does. Instead, take advantage of the methods on the ThreadLocalRandom class:
double randomGenerator() {
return ThreadLocalRandom.current().nextDouble(0.5);
}

C# needing a six digit number beginning with two 0's

I'm making a program where products are searched for by there batch number. I want to the batch number to be a random 6 digit code starting with 00, for example 002142; at the moment it is only making a random 6 digit code.
Here is the code I have tried:
public string getRandomNumber()
{
Random random = new Random(100000);
string randomNumber = random.Next(999999).ToString("D6");
return randomNumber;
}
public string getRandomNumber()
{
Random random = new Random();
string randomNumber = "00"+random.Next(10000).ToString("D4");
return randomNumber;
}
You should not set a fix seed in the Random, because it will allways generate the same random number sequence. For example if you call your function in a loop it will allways return the same number, thus not being random.
Also the parameter in the Next() function is the upper bound exclusive so use 10000 instead 9999 so you can get 9999 as a random number as well.
why is everybody creating 6 digit ? what really is needed is create 4 and concat.
int random = new Random().Next(1000, 9999);
string code = "00" + random.ToString();
EDIT:
Thanks for marking as correct answer but my code is wrong. If you don't pass minimum value to next method, you can get numbers from 1 to 999 which you don't want. I edited my answer. Hope it's not late for anything.
You wrote .Next(999999) so i assume you want random values from 0 to 999999.
First you should write 1000000 because .Next() determines a random number from 0 to exclusive the input value.
Then you should
replace
string randomNumber = random.Next(1000000).ToString("D6");
with
string randomNumber = random.Next(999999).ToString().PadLeft(6, '0');
to fill the missing digits with 0
Another possibility is "000000" format string (six digits, leading zeros are mandatory):
random.Next(10000) - last 4 digits are random
ToString("000000") - 6 digits are returned (at least 2 leading zeros)
Implementation
string randomNumber = random.Next(10000).ToString("000000");
Another issue is that you should not declare random locally:
// Simplest, but not thread safe
private static random = new Random();
...
public static string getRandomNumber()
{
return random.Next(10000).ToString("000000");
}
You were close. What you really want is a random four digit number, padded with two leading zeroes (to six places) . So use Random.Next(10000).ToString("D6") or . ToString("000000")
Note however for numbers less than 1000, you will end up with more than two leading zeroes. To avoid that, you can do Random.Next(1000,10000) where the first number is inclusive, and the second exclusive.
Note also that the way you are initializing the random is incorrect if you are going to be calling this method more than once! Make your Random object a class member (preferably static) and only seed/initialize it once (don't pass 100000 to its constructor) Otherwise if this method is called more than once, it is going to return the same value each time.
Use new Random() . If you are going to seed it the way you do, it must be a static variable (or at least a class member--however for each instance of the class you will end up with the same values being generated)
I would do something like this:
var rnd = new Random(); // the internal seed is good enough
var rndNums = string.Join("", Enumerable.Range(0, 4).Select(x => rnd.Next(10)));
return "00" + rndNums;
Then you can easily change the amount you want, like this:
string GetRandomBatch(int numberOfRandomNumbers)
{
var rnd = new Random(); // the internal seed is good enough
var rndNums = string.Join("", Enumerable.Range(0, numberOfRandomNumbers).Select(x => rnd.Next(0, 9)));
return "00" + rndNums;
}
With interpolated string (C# 6.0) you can do it like this :
Random random = new Random();
string randomNumber = $"00{random.Next(9999)}";
Your ToString("D6") does exactly what it appears that you want, but since you are passing a large upper bound for your random.Next, there will in many scenarios not be anything to pad.
As others have indicated, you may prefix "00", or you could simply set a lower upper bound, since the "D6" will take care of your padding:
string randomNumber = random.Next(9999).ToString("D6");
If you want the number to always be 4 digits and not, say, "000123", you'll need to specify a lower bound as well:
string randomNumber = random.Next(1000, 9999).ToString("D6");
Also note that if you are specifying a hardcoded seed as in your example, you will always get the same random number back.
Something like this will do.
public static void Main()
{
Random rnd = new Random();
string Value = rnd.Next(1, 9999).ToString("D6");
Console.WriteLine(Value);
}
Or with lower possibility of getting the same number would be
public static void Main()
{
Random rnd = new Random(DateTime.Now.Millisecond);
string Value = rnd.Next(1, 9999).ToString("D6");
Console.WriteLine(Value);
}
public string RandomNum()
{
return "00"+ new Random().Next(9999).ToString("D4");
}
you can also give minimum and maximum number to Next() function. Like:
return "00"+ new Random().Next(1,9999).ToString("D4");

Need to generate different random numbers

I need help in trying to generate a random number because with my coding below it shows the same number in both text boxes.
private int RandomNumber(int min, int max)
{
Random random = new Random();
random.Next();
return random.Next(1, 7); // random integer and assigned to number
}
private void button1_Click(object sender, EventArgs e)
{
tb1.Text = RandomNumber(1, 7).ToString(); // Random Number for Text Box 1.
tb2.Text = RandomNumber(7, 1).ToString(); // Random Number for Text Box 2.
}
Random picks a seed based on the current time.
If you create two Randoms at the same time, they will give you the same numbers.
Instead, you need to create a single Random instance and store it in a field in your class.
However, beware that Random is not thread-safe.
You need to instantiate your Random class only once. From MSDN, the documentation states that:
If the same seed is used for separate Random objects, they will generate the same series of random numbers.
In your case, as SLaKs also said, the seed is the current time. You're calling the functions so close together they are using the same seed. If you move the instantiation outside of the function, you have one instance based on one seed, instead of multiple objects based on the same seed.
Random random = new Random();
private int RandomNumber(int min, int max)
{
return random.Next(1, 7); // random integer and assigned to number
}
You need to create the Random object outside of your function. Creating a new one each time you need a new random number will result in the seed being identical (given the time gap between creations)

Why does Random.Next() always return the same number [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 7 years ago.
Consider this method:
private static int GenerateRandomNumber(int seed, int max)
{
return new Random(seed).Next(max);
}
On my machine, executing this loop yields the same number through 1500 iterations:
for (int i = 0; i < 1501; i++)
{
int random = GenerateRandomNumber(100000000, 999999999);
Console.WriteLine(random.ToString());
Console.ReadKey();
}
I get 145156561, for every single iteration.
I don't have a pressing issue, I was just curious about this behavior because .Next(max) says "Returns a Non Negative random number less than the specified maximum. Perhaps I am not understanding something basic.
You're always seeding a new instance with the same seed, and then grabbing the first max. By using a Seed, you're guaranteeing the same results.
If you want to have a static, random number generation that does different results, you should rework this a bit. However, since Random is not threadsafe, it requires some synchronization when used statically. Something like:
private static Random random;
private static object syncObj = new object();
private static void InitRandomNumber(int seed)
{
random = new Random(seed);
}
private static int GenerateRandomNumber(int max)
{
lock(syncObj)
{
if (random == null)
random = new Random(); // Or exception...
return random.Next(max);
}
}
Dilbert has encountered the same problem back in 2001:
http://dilbert.com/strips/comic/2001-10-25/
Coincidence?
I don't think so.
And random.org agrees : http://www.random.org/analysis/
The problem is that you are creating a new Random instance with the same seed number each time. You should create a single Random instance (store it in a static if necessary) and simply call the next method on that same instance.
Random number generation is not truly random, see this Wikipedia entry for more details.
Salam to All,
Well it drove me crazy as well. The answer is simple. Change the seed before you generate random.
Example:
I want to generate random number between 1 to 10
Random rnd = new Random(DateTime.Now.Second);
int random_number = rnd.Next(10);
Put it inside a loop and run it three times. It will give out random numbers below 10.
Pseudo-random number generator usually work by choosing a seed, and then generating a deterministic sequence based on that seed. Choosing the same seed every time, you generate the same sequence.
There are "only" 2^32 different random sequences in .NET.
Not sure how the internals work.. check wiki for it, but it's very simple.
public class MathCalculations
{
private Random rnd = new Random();
public Int32 getRandom(Int32 iMin, Int32 iMax)
{
return rnd.Next(iMin, iMax);
}
}
public class Main
{
MathCalculations mathCalculations = new MathCalculations();
for (int i = 0; i < 6; i++)
{
getRandom(0,1000);
}
}
will generate Number1, Number2, Number3, Number4, Number5, Number6 (1 seed, 1 sequence of many numbers, random*not really, but approx.*)
if you however do this:
public class MathCalculations
{
public Int32 getRandom(Int32 iMin, Int32 iMax)
{
Random rnd = new Random();
return rnd.Next(iMin, iMax);
}
}
public class Main
{
MathCalculations mathCalculations = new MathCalculations();
for (int i = 0; i < 6; i++)
{
getRandom(0,1000);
}
}
You will now get Number1, Number1, Number1, Number1, Number1, Number1 (1 seed, 6 equal sequences of many numbers, always pick the same starting number from each equal sequence).. At some point Number1 will be different, because the seed changes over time.. but you need to wait some time for this, nonetheless, you never pick number2 from the sequence.
The reason is, each time you generate a new sequence with the same seed, hence the sequence is the same over and over again, and each time your random generated will pick the first number in it's sequence, which, with the same seed, is of course always the same.
Not sure if this is technically correct by the underlying methods of the random generator, but that's how it behaves.
In the event that anyone is looking for a "quick and dirty" "solution" (and I use that term with caution) then this will suffice for most.
int secondsSinceMidnight = Convert.ToInt32(DateTime.Now.Subtract(DateTime.Today).TotalSeconds);
Random rand = new Random(secondsSinceMidnight);
var usuallyRandomId = rand.Next();
Please note my use of usually random. I agree that the item marked as the answer is a more correct way of doing this.

C# code only gives expected results on step through?

Ok so I have a dice throw app...
When I step through the code it functions normally and 'results' contains the correct number of throw results and they appear to be random, when I leave the code to run and do exactly the same thing it produces a set of identical numbers.
I'm sure this is a logical error I cannot see but fiddling with it for hours hasnt improved the situation, so any help is much appriciated. :)
class Dice
{
public int[] Roll(int _throws, int _sides, int _count)
{
Random rnd = new Random();
int[] results = new int[_throws];
// for each set of dice to throw pass data to calculate method
for (int i = 0; i < _throws; i++)
{
int thisThrow = Calculate(_sides, _count);
//add each throw to a new index of array... repeat for every throw
results[i] = thisThrow;
}
return results;
}
private int Calculate(int _sides, int _count)
{
Random rnd = new Random();
int[] result = new int[_count];
int total = 0;
//for each dice to throw put data into result
for (int i = 0; i < _count; i++)
{
result[i] = rnd.Next(1, _sides);
}
//count the values in result
for (int x = 0; x < _count; x++)
{
total = total + result[x];
}
//return total of all dice to Roll method
return total;
}
}
First mistake: Never use multiple instances of Random, use a single instance, and pass that along with the other parameters.
When you create "Random rnd = new Random();" it is seeded by the current time. When you debug your code (which takes time) it will be seeded differently each time.
Create 1 instance of Random, and reference that everywhere.
You're creating a random class every time you need to create a number. Doing this will give you the nutty results.
See here: FROM MSDN
This problem can be avoided by creating a single Random object rather than multiple ones.
To improve performance, create one Random object to generate many random numbers over time, instead of repeatedly creating a new Random objects to generate one random number.
E.g. create a private instance of Random...
In addition to what has been mentioned before...
Use Random for things like dice, card games, choosing random images and so forth. If you ever need to create a random number for security sake, use System.Security.Cryptography.RandomNumberGenerator. This simple example shows creating a random integer.
RandomNumberGenerator gen = RandomNumberGenerator.Create();
byte[] myBytes = new byte[4];
gen.GetBytes(myBytes);
int myValue = (BitConverter.ToInt32(myBytes, 0));
DO NOT use this unless you have a security need. The performance is less than that of the Random class. I suppose you could use this to seed Random but that might be overkill.
EDIT: It occurred to me that I had never tested this. A quick performance test showed the following:
1,000,000 random numbers:
RandomNumberGenerator: 2.6 seconds
Random: .015 seconds.
So Random is about 150 times faster.
Give the constructor Random a seed. That's the problem.
http://msdn.microsoft.com/en-us/library/aa329890%28VS.71%29.aspx
Random r = new Random(DateTime.Now.Millisecond);

Categories

Resources