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 7428493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:48:34+00:00 2026-05-29T08:48:34+00:00

I am having problem with random number generation in c#. If I RUN this

  • 0

I am having problem with random number generation in c#. If I RUN this form application directly, the random number generation is same for all.

If i DEBUG this line by line by pressing F10, then it will generate different random numbers. Why this is happening? What should I do to generate different random numbers?

Greyhound.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADayAtTheRaces
{
    class Greyhound
    {
        int location=0;
        Random randomize = new Random();

        public int run()
        {
            location = randomize.Next(0, 100);
            return location;
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADayAtTheRaces
{
    public partial class Form1 : Form
    {
        Greyhound[] greyHound = new Greyhound[4];
        ProgressBar[] progressBar = new ProgressBar[4];
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 4; i++)
            {
                greyHound[i] = new Greyhound();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= 3; i++)
            {
                progressBar[i].Value = greyHound[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-29T08:48:34+00:00Added an answer on May 29, 2026 at 8:48 am

    Don’t instantiate a new Random object each time, instead use it as a static member:

    class Greyhound
    {
        static Random randomize = new Random();
        int location=0;
    
        public int run()
        {
            location = randomize.Next(0, 100);
            return location;
        }
    }
    

    See Random.Next returns always the same values

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem requesting a random number from random.org using jQuery. When
We're having problem with a huge number of legacy stored procedures at work. Do
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
We are having problem with the server migration. We have one application that are
I'm having problem selecting an element with an id like this <li =0f:Bactidol_Recorder.mp4>. I
I've build a random string generator but I'm having a problem whereby if I
First time poster, long time reader. I've been having a problem with figuring this
This is part of a bigger problem, but I'm having some problems with this
I must generate a random number using boost libraries, I use this code: boost::mt19937
I am currently having the problem of not able to run the following code

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.