Randomization on a Logical System

Short version of this post: Randomization on a computer system is totally impossible, any “Randomization” you do see is purely clever deception.

Today I’ve been writing the Start Game logic for Sweepy Cleaner. One of the things I have to do during the start of a game is randomize the position of 5 pieces of dirt, and I have spent some of the night tinkering with getting things as random as possible. I say as random as possible because making random numbers is impossible, a number is selected based off of the exact time of the System clock in your PC or on your mobile device. You can prove this with the following C# code:


for(int i = 0; i < 1000; i++)

{

Random r = new Random();

int randomInt = r.Next(0, 3000);

Console.WriteLine("Random Number: " + randomInt);
}

For me this outputted “Random Number:  1742” around 500 times and “Random Number: 2818” around 500 times. This is because this entire loop takes place in just a few milliseconds, not allowing for much change in the System clocks time.  Producing lots of identical numbers in a short space of time can be a pain, especially if you want to initialize a game quickly but want random locations for dirt like I do.  The solution I found is simple, pause the thread for a “Random” amount of time as well before each pass.


Random RandomX = new Random();

System.Threading.Thread.Sleep(new System.TimeSpan(RandomX.Next(0,7)));
x = RandomX.NextDouble() * (RandomX.Next(0, 800));

System.Threading.Thread.Sleep(new System.TimeSpan(RandomX.Next(0,8)));
Random RandomY = new Random();
y = RandomY.NextDouble() * (RandomY.Next(0, 480));

This makes things a little more random but as I said at the beginning Randomization on a computer system is totally impossible. If this makes little sense to you think about what a computer is (an arithmetical and logical processor), there is no such thing as random in Logic, hence there can’t be in a logic system. 🙂

Danny.

Advertisements

Tags: , , , , , , ,

4 responses to “Randomization on a Logical System”

  1. JBL says :

    It’s actually a seeding issue. You just need to use a new seed every time. Give this a go

    Modify the code to this:

    Random r = new Random();

    for(int i = 0; i < 1000; i++)

    {

    int randomInt = r.Next(0, 3000);

    Console.WriteLine("Random Number: " + randomInt);
    }

    JBL

  2. JBL says :

    To clear up what I just said..Random.next() gives you the next number of the random number sequence starting at a specific seed. If you always give it the same seed it will and you take the first Next() after generating it.

    The Random seed is set up when Random is constructed and is seeded from the clock. Your loop runs so fast the seed number doesn’t change so you are just taking the first number in the sequence starting from he same seed as the time before.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s