Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with Random class in C#

Problem with Random class in C#

Scheduled Pinned Locked Moved C#
helpcsharplounge
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yadlaprasad
    wrote on last edited by
    #1

    HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.

    fttyhtrhyfytrytrysetyetytesystryrty

    C P P K 4 Replies Last reply
    0
    • Y yadlaprasad

      HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.

      fttyhtrhyfytrytrysetyetytesystryrty

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      The problem is, that new Random(); uses the current time to set the starting seed. If you call this in a too short time period twice it returns the same random number. Use something like this:

      public class ...
      {

      private int RandomNumber(int min, int max)
      {
          return m\_rndGen.Next(min, max);
      }
      
      private Random m\_rndGen = new Randow();
      

      }

      Greetings Covean

      1 Reply Last reply
      0
      • Y yadlaprasad

        HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.

        fttyhtrhyfytrytrysetyetytesystryrty

        P Offline
        P Offline
        Phil J Pearson
        wrote on last edited by
        #3

        Don't instantiate a new Random every time. Treat random as a singleton. Random will (by design) give you the same sequence of numbers every time unless you start with a different seed. Right now you are getting the first number of the sequence every time.

        Phil


        The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

        1 Reply Last reply
        0
        • Y yadlaprasad

          HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.

          fttyhtrhyfytrytrysetyetytesystryrty

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Make the Randon instance a (static) member of the class.

          1 Reply Last reply
          0
          • Y yadlaprasad

            HI, i am using Random class in my code like below private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } and i am clalling this method in for loop. my expectation is for every iteration in the loop i am expecting some new random number. code for that is like below: string waybill= "W" + RandomNumber(10000, 90000).ToString(); Problem: everytime i am gettting same random number fr every iteration. instead of generating different random number. can any one help me , where i done the mistake.

            fttyhtrhyfytrytrysetyetytesystryrty

            K Offline
            K Offline
            Kythen
            wrote on last edited by
            #5

            The default constructor for the Random class uses the system clock to seed the random number generator. Your code is running fast enough that the system clock has not changed between calls to the constructor, resulting in the same random numbers being generated each iteration. See the MSDN documentation for the Random constructor[^] for more clarification and a good example.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups