O'Reilly Forums: Day At The Races Problem With Randomizer - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Day At The Races Problem With Randomizer

#1 User is offline   Amazing Larry 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 22-August 14

Posted 22 August 2014 - 02:40 PM

I wrote that code at the bottom of page 190 for each of my greyhounds, but get a squiggle line under MyRandomizer. The error is "The name 'MyRandomizer' does not exist in the current context". I tried various things but can't seem to fix it. I guess I dont even understand what the Randomizer = MyRandomizer line is for.

I also don't understand what it means when it says "You only need one instance of Random - each greyhounds randomizer reference should point to the same Random object."

If there should only be one instance of random, why is it contained in a class that is going to have multiple instances?

Anyway, for my first question here is my current code for Form1:


public partial class Form1 : Form
    {
        Greyhound[] GreyhoundArray = new Greyhound[3];
        Guy[] GuyArray = new Guy[2];

        public Form1()
        {
            GreyhoundArray[0] = new Greyhound() 
            { 
                MyPictureBox = dogPictureBox1,
                StartingPosition = dogPictureBox1.Left,
                RacetrackLength = racetrackPictureBox.Width - dogPictureBox1.Width,
                Randomizer = MyRandomizer
            };

            GreyhoundArray[1] = new Greyhound()
            {
                MyPictureBox = dogPictureBox2,
                StartingPosition = dogPictureBox2.Left,
                RacetrackLength = racetrackPictureBox.Width - dogPictureBox2.Width,
                Randomizer = MyRandomizer
            };

            GreyhoundArray[2] = new Greyhound()
            {
                MyPictureBox = dogPictureBox3,
                StartingPosition = dogPictureBox3.Left,
                RacetrackLength = racetrackPictureBox.Width - dogPictureBox3.Width,
                Randomizer = MyRandomizer
            };

            GreyhoundArray[4] = new Greyhound()
            {
                MyPictureBox = dogPictureBox4,
                StartingPosition = dogPictureBox4.Left,
                RacetrackLength = racetrackPictureBox.Width - dogPictureBox4.Width,
                Randomizer = MyRandomizer
            };

            GuyArray[0] = new Guy()
            {
                Name = "Joe",
                Cash = 50,
                MyRadioButton = joeRadio,
                MyLabel = joeBetLabel,
                MyBet = null 
            };

            GuyArray[1] = new Guy()
            {
                Name = "Bob",
                Cash = 50,
                MyRadioButton = bobRadio,
                MyLabel = bobBetLabel,
                MyBet = null  
            };

            GuyArray[2] = new Guy()
            {
                Name = "Frank",
                Cash = 50,
                MyRadioButton = frankRadio,
                MyLabel = frankBetLabel,
                MyBet = null
            };

            for (int i = 0; i < GuyArray.Length; i++)
            {
                GuyArray[i].UpDateLabels();
            }

            minBetsLabel.Text = "Minimum Bet: " + minBetSelect.Minimum + " dollars";

            InitializeComponent();
        }

        private void raceButton_Click(object sender, EventArgs e)
        {
            while (true)
            {
                for (int i = 0; i < GreyhoundArray.Length; i++)
                {
                    GreyhoundArray[i].Run();
                }
            }
        }

        private void betButton_Click(object sender, EventArgs e)
        {

        }
    }

This post has been edited by Amazing Larry: 22 August 2014 - 03:00 PM

0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 806
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 22 August 2014 - 03:47 PM

Quote

I wrote that code at the bottom of page 190 for each of my greyhounds, but get a squiggle line under MyRandomizer. The error is "The name 'MyRandomizer' does not exist in the current context". I tried various things but can't seem to fix it. I guess I dont even understand what the Randomizer = MyRandomizer line is for.


You're getting that error because MyRandomizer isn't defined anywhere. Try adding a field to the form, like this:
public partial class Form1 : Form
        Greyhound[] GreyhoundArray = new Greyhound[3];
        Guy[] GuyArray = new Guy[2];
        Random MyRandomizer = new Random();



Quote

I also don't understand what it means when it says "You only need one instance of Random - each greyhounds randomizer reference should point to the same Random object."


It means that there's just one single Random object, and each Greyhound object has a reference that points to it. So when you create the new Random object with "new Random()" and assign its Randomizer field to point to it ("Randomizer = MyRandomizer"), you've only created the one Random object, and each Greyhound uses it.

If you put this in each Greyhound's object initializer instead:
Randomzier = new Random()

then you would be creating a new Random object for each Greyhound.

I hope this helps!

Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#3 User is offline   rchojwa 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 27
  • Joined: 25-August 14

Posted 25 August 2014 - 07:35 AM

Quote

then you would be creating a new Random object for each Greyhound.



and what wrong would that lead to? only more memory occupation or just a not consistent random choice?


thanks
0

#4 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 806
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 25 August 2014 - 09:13 AM

View Postrchojwa, on 25 August 2014 - 07:35 AM, said:

and what wrong would that lead to? only more memory occupation or just a not consistent random choice?


The Random class uses system clock as a random number seed. If you create a new Random for each Greyhound, .NET creates different instances of Random so quickly that they sometimes all get the same random number seed, so when each instance calls its Next() method it will get exactly the same number. Sharing the same instance of Random guarantees that they'll get different numbers because they'll be the next number in a pseudorandom sequence.

We intentionally did not go into this detail in the book, because you don't really need to understand the pseudorandom number algorithm of the .NET Random class in order to do the lab. As long as each Greyhound has a reference to the same Random object, it'll work just fine.



You can learn more about the Random class here: http://msdn.microsof...tem.random.aspx (see the Remarks section for more about how it seeds random numbers).
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#5 User is offline   rchojwa 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 27
  • Joined: 25-August 14

Posted 25 August 2014 - 10:28 PM

thank you Andrew

that was what I actually ment for the "not consistent random choice" option
0

#6 User is offline   Amazing Larry 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 22-August 14

Posted 26 August 2014 - 11:24 AM

Thank you Andrew!

Its very difficult moving from self contained console applications to these programs that span lots of different files/classes etc.. Trying to figure out what needs to go where when it is not all a nice straight line is tough.

I was getting pretty cocky making some real nifty console apps.. this object oriented stuff has really put me in my place. It's funny because in theory it's extremely clear and obvious, but in practice you begin to see all the tiny details that need to be dealt with and it's gets complicated real fast.
0

#7 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 806
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 27 August 2014 - 06:07 PM

Here's a timely post from another reader who ran into exactly the problem I mentioned: http://forums.oreill...post__p__119039
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users