Share via


Random thoughts on Randomness

Attempting to be very random, I have been thinking about what a Random number is.  One way to generate a random number would be to calculate the number PI out to 1,000,000 digits and then roll a number of dice.  From the result of the dice roll select that point on the list of numbers from PI, say 9 numbers after your “seed” generated by your dice roll, now you have a nice random number.  Wow.  Really?  Why don’t I just roll the dice 9 times?  Which is more random?  

What about these numbers:

49 52 42 34 67 51 79 55 26 2 17 48 86 77 46

image Are they random?  How do I tell?  Will I have to take statistics again?

Calm down, random numbers that are truly random have a uniform distribution. 

For example: Those sharply cornered dice at Las Vegas have a probability of 1/6 of rolling 1, 1/6 of rolling a 2, and so forth. 

Simple.  Do I care?  If you are at a casino, you might, if you are playing Monopoly, maybe not so much.

How to generate random numbers in XNA 4.0, which may or may not have a uniform distribution (you to decide).

I simply assume that the numbers generated are close to random:

First you have to create a memory to store the random generator object:

//********************Start Code******************

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont Font1;
        Vector2 FontPos;
        /***********************************************************
         * create a memory location for the random object
         **********************************************************/
        Random rand;
        /*********************************************************/

        StringBuilder sbuilder;
        Byte[] randArray;
        int intArraySize = 10;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Pre-autoscale settings.
            graphics.PreferredBackBufferWidth = 480;
            graphics.PreferredBackBufferHeight = 800;
            /***********************************************************
             * Construct the class for the random object
             **********************************************************/
            rand = new Random();
        }

/***************************************************

Then in one of the methods, you will need to use the random object, in this case I am using the NextBytes method and passing an integer that is called randArray, which is current 10 in this example.  The NextBytes method in the random class allows you to generate an array of random numbers.:

***************************************************/

private void StringRandomDice()
        {
            // Generate Random Numbers as a string
            rand.NextBytes(randArray);
            sbuilder.AppendLine();
            sbuilder.AppendLine("Example of screen print and " + (char)10 + (char)13 + "random.NextBytes");

            sbuilder.AppendLine("-Turn-|---Random Nr ----");

            for (int i = 0; i < intArraySize; i++)
            {
                sbuilder.AppendLine(" " + i + "             " + randArray[i]);
                sbuilder.AppendLine("---------|-------------------");
                //output1 = output1 + " " + i +
                //          "       Dice1= " + rand.Next(1, 6) +
                //          " Dice2 = " + rand.Next(1, 6) + " " + '\x000A' + '\x000D';
            }
        }

//*******************end code *****************************

Other methods available in the random class are:

rand.Next();                            //Returns a non-negative integer
rand.Next(intMax);                  //Returns a non-negative integer up to the integer entered
rand.Next(intMin, intMax);       //Returns a non-negative integer between the two integers
rand.NextDouble();                 //Returns a float between 0.0 and 1.0

So to those few readers who feel that my blogs are random, this one is indeed random.