Unity3d generate multiple random numbers - c#

I am currently attempting to generate two random (whole) numbers at once for a game I am making. I am trying to randomly generate a start and end tile. When I do this though, my two randomly generated numbers are always the same. I understand this is probably just because they are being called calculated to close together, but I can't find any information that works on how to get two different random numbers. my code is quite simple (in c#):
float startTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));
float endTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));
Thank you in advance!
Edit
This was marked as a duplicate question, where the answer for the old question was:
Random rand = new Random();
int t = rand.next(x,x2);
Unless I'm doing something incorrect, I am unable to even use the rand.Next(x,x1) function within unity.
The actual code:
private void SpawnStartandEndTiles()
{
Random rand = new Random();
int t = rand.N //<Next is not an option
//Other misc. Code that uses the random numbers
}
Please let me know if I am just not getting something with how the Random function works.
Thanks so much.

First of all don't use System.Random because using most classes in System namespace (*) drastically increase the size of your game. Instead use UnityEngine.Random
Random numbers are always generated using a seed. Given a specific seed the sequence of random numbers are always the same. You can say computed random is never really random.
By setting the Random.seed to a value depending on the time makes Random.Range to generate numbers close to real random.
For example:
Random.seed = System.DateTime.Now.Millisecond;
//...
float startTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));
float endTile = Mathf.Ceil(Random.Range(1.0f, 8.0f));
(*) These are in mscorlib.dll and therefore does not have any impact on the size of game
System.Collection.*
System.Collection.Generic.*
System.IO.*

Related

How can I pick a random entry from a list?

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.

Random Number Generator using Geometric Distribution

I need a random number generator using a geometric distribution
http://en.wikipedia.org/wiki/Geometric_Distribution.
I tried MathNet.Numerics.Distributions:
public void GeometricTest()
{
var geometric = new Geometric(0.1);
int back = geometric.Sample();
Assert.Greater(back, -1);
}
But the test gives just negative numbers. Does somebody spot my mistake or give me advice for other ways of sampling a geometric distribution?
To generate a geometric with probability p of success on each trial, given a function rand which returns a uniform(0,1) result, pseudocode is:
define geometric(p)
return ceiling(ln(1-rand) / ln(1-p))
This yields how many trials until the first success. If you want the alternate definition of a geometric (how many failures prior to the first success) subtract 1 or use floor instead of ceiling.

C# For Loop being temperamental

For some reason, the loop only prints 1-3 circles to the panel unless the MessageBox.Show is called. it should print the number of circles the user input into the textbox. I don't understand why that MessageBox being called matters, why the loop decides not to do more than a few circles if the Box is not called. it should do the full number of loops around asked.
private void button2_Click(object sender, EventArgs e)
{
if (NumberCirclesText.Text != "" && Convert.ToInt32(NumberCirclesText.Text) < 100)
for (int x = 0; x < Convert.ToInt32(NumberCirclesText.Text); x++)
{
int y = x + 1;
Random myrandom = new Random();
int xcoord;
int ycoord;
ycoord = myrandom.Next(DrawSpace.Width);
xcoord = myrandom.Next(DrawSpace.Height);
int r = myrandom.Next(255);
SolidBrush mybrush = new SolidBrush(Color.FromArgb(myrandom.Next(255), myrandom.Next(255), myrandom.Next(255)));
System.Drawing.Graphics graphics = DrawSpace.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(ycoord, xcoord, 30, 30);
graphics.DrawEllipse(System.Drawing.Pens.Transparent, rectangle);
graphics.FillEllipse(mybrush, rectangle);
//MessageBox.Show("There are " + y + " circles on the panel.");
NoOfCircles.Text = y.ToString();
}
}
The message box is a red-herring: it's the fact the message box causes a non-trivial delay that things work. You're creating a new Random object each iteration, but each iteration is so fast, the objects will produce the same "random" sequence. With the same "random" inputs, the circles appear (mostly) overlapped.
The simple fix, then, is to just declare and create the Random object outside the loop.
TL;DR Version: just put the line Random myrandom = new Random(); before the loop.
To understand the cause of this you first need to understand how computers generate random numbers:
Since a computer is not a very random machine, it cannot generate true random numbers, it can only generate pseudo-random numbers - numbers that seem random. The way computers do this is when given a starting value (called a seed), they apply some first-grade maths to it, and get a pseudo-random number. When asked to generate another number, they simply apply the actions they did to the starting value to the previously generated number.
Now that you know how computers generate numbers that look like they are random, you may have one question - Where does the starting value come from?
Well, since when given the same starting value the computer will generate the same sequence of random numbers, we need some value that constantly changes. What changes any second? Time! Computers use the amount of milliseconds since epoch (usually January 1st, 1970) as their seed to generate random numbers.
In .NET (which includes C#), the way to tell the computer to use the time to generate a random sequence, is by creating a new Random object, Random myrandom = new Random();
To get the next number in the sequence, you call myrandom.Next();.
Since your code loops and creates a new Random object every iteration, and since every iteration is fast enough so the time does not change, each new Random object has the same seed, and so when you're asking it for a random number it gives you the first in its sequence. Because the seed of all the Random objects is the same, the first random number each one generates will be identical.
Now after you've understood the problem, the solution:
In order to get different random numbers each time you can do one of the following:
Either have each new Random object have a different seed, which means
at least a millisecond has to pass before the for loop repeats.
Or just use one Random object (its seed is the time when the for loop
starts) and every time you want a random number just ask the same
object for the next number in the sequence.
Since the first approach requires you to slow down your loop, which is stupid unless you don't have another option, choose the second solution, which is to declare the Random object outside the loop.

Creating a true random [duplicate]

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.

Random numbers for dice game [duplicate]

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.

Categories

Resources