Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6138183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:49:09+00:00 2026-05-23T17:49:09+00:00

i’m new at C # and i’m creating a racetrack simulator, but i’m currently

  • 0

i’m new at C # and i’m creating a racetrack simulator, but i’m currently encountering some issues when i run my code. I have an array of four Greyhound objects, and if I call Greyhound.Run on my form1.cs and i don’t have a “MessageBox.Show(“Distance” + distance)” on my Run method, which shows me how many pixels each greyhound is supposed to move, all of the greyhounds end up moving the same distance. I don’t understand why this is happening

namespace Race
{
    class Greyhound
    {
        public int StartingPosition;
        public int RacetrackLength;
        public PictureBox MyPictureBox = null;
        public int Location = 0;
        public Random Randomizer;

        public bool Run()
        {
            Point p = MyPictureBox.Location;
            if (p.X + MyPictureBox.Width >= RacetrackLength)
            {
                //TakeStartingPostion();
                return true;

            }
            else
            {
                Randomizer = new Random();
                int distance = Randomizer.Next(100);
                MessageBox.Show("Distance is " + distance);
                p.X += distance;
                MyPictureBox.Location = p;
                return false;
            }

        }

        public void TakeStartingPostion()
        {
            Point P = MyPictureBox.Location;
            P.X = StartingPosition;
            MyPictureBox.Location = P;
        }
    }
}


namespace Race
{
    public partial class Form1 : Form
    {
        Guy[] guys = new Guy[3];
        Greyhound[] hounds = new Greyhound[4];

        public Form1()
        {
            InitializeComponent();

            hounds[0] = new Greyhound()
            { 
                StartingPosition = 12,
                MyPictureBox = GreyHound1,
                RacetrackLength = 636
            };

            hounds[1] = new Greyhound()
            { 
                StartingPosition = 12,
                MyPictureBox = GreyHound2,
                RacetrackLength = 636
            };

            hounds[2] = new Greyhound()
            { 
                StartingPosition = 12,
                MyPictureBox = GreyHound3,
                RacetrackLength = 636
            };

            hounds[3] = new Greyhound()
            { 
                StartingPosition = 12,
                MyPictureBox = GreyHound4,
                RacetrackLength = 636
            };
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < hounds.Length; i++)
            {

                 hounds[i].Run();
            } 

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:49:10+00:00Added an answer on May 23, 2026 at 5:49 pm

    It’s because you’re calling new Random() each time the Run() method hits the else block. The default Random constructor initializes the pseudorandom number generator based on the current time. When there’s no interruption, all 4 method run at “the same time”, so they spit out the same random numbers. To fix this, either create only a single Random, or else use a different seed for each one (by using the Random constructor that takes a seed as a parameter.)

    Something like this would work:

    public class Greyhound
    {
        public static Random randomizer = new Random();
    
        // ... In the run method ...
        int distance = Greyhound.randomizer.Next(100);
    
    }
    

    Update: As Groo points out, if you were actually calling Next() from multiple threads, the code I’ve shown isn’t thread-safe. While that’s not the case with your code, it’s a good idea to be aware of issues like this earlier rather than later. The general solution to this (potential) problem is to surround the call to Next() with a lock, like so:

    // ... After the declaration of randomizer ...
    private static object randomLock = new object();
    
    // ... New Next() call...
    lock (randomLock)
        Greyhound.randomizer.Next(100);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.