# 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.

### 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

• dannycomputerscientist says :

Your solution is much more elegant and successful than mine, ill substitute it in to the original post now. 🙂 Thanks.

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.

• cameronwilby says :

Came here to say this!

This site uses Akismet to reduce spam. Learn how your comment data is processed.