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. Same Random Numbers

Same Random Numbers

Scheduled Pinned Locked Moved C#
lounge
23 Posts 8 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.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    This is the piece of code

    Random rnd = new Random();
    Random rnd2 = new Random();
    for (int i = 0; i < 5; i++)
    {
    myIntegerCollection.Add(i);
    myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd2.Next(50))));
    }

    both the Random instances rnd and rnd2 are generating the same numbers over and over again, and I dont understand why.

    D E P R 4 Replies Last reply
    0
    • H humayunlalzad

      This is the piece of code

      Random rnd = new Random();
      Random rnd2 = new Random();
      for (int i = 0; i < 5; i++)
      {
      myIntegerCollection.Add(i);
      myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd2.Next(50))));
      }

      both the Random instances rnd and rnd2 are generating the same numbers over and over again, and I dont understand why.

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Use the Next method on your Random instances.

      Dave

      H 1 Reply Last reply
      0
      • D DaveyM69

        Use the Next method on your Random instances.

        Dave

        H Offline
        H Offline
        humayunlalzad
        wrote on last edited by
        #3

        Thats what I have done Dave. rnd.Next(50) rnd2.Next(50)

        L D 2 Replies Last reply
        0
        • H humayunlalzad

          Thats what I have done Dave. rnd.Next(50) rnd2.Next(50)

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          They are both created at the same time, so the seed is the same. Create them with different seeds.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          H 1 Reply Last reply
          0
          • H humayunlalzad

            Thats what I have done Dave. rnd.Next(50) rnd2.Next(50)

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            either do what Leppie suggests above or just use one Random and call rnd.Next(50) in place of rnd2.Next(50) so you only need the one seed.

            Dave

            1 Reply Last reply
            0
            • L leppie

              They are both created at the same time, so the seed is the same. Create them with different seeds.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              H Offline
              H Offline
              humayunlalzad
              wrote on last edited by
              #6

              Even this does not help

              Random rnd = new Random();
              Random rnd2 = new Random();
              int newNo = 0;
              for (int i = 0; i < 5; i++)
              {
              newNo = rnd2.Next(50);
              myIntegerCollection.Add(i);
              myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name" + newNo)));
              }

              L 1 Reply Last reply
              0
              • H humayunlalzad

                Even this does not help

                Random rnd = new Random();
                Random rnd2 = new Random();
                int newNo = 0;
                for (int i = 0; i < 5; i++)
                {
                newNo = rnd2.Next(50);
                myIntegerCollection.Add(i);
                myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name" + newNo)));
                }

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                You clearly did not listen to what I said. Now go through the docs and look for seed, and the constructor for the Random class.

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                H 2 Replies Last reply
                0
                • H humayunlalzad

                  This is the piece of code

                  Random rnd = new Random();
                  Random rnd2 = new Random();
                  for (int i = 0; i < 5; i++)
                  {
                  myIntegerCollection.Add(i);
                  myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd2.Next(50))));
                  }

                  both the Random instances rnd and rnd2 are generating the same numbers over and over again, and I dont understand why.

                  E Offline
                  E Offline
                  erfi
                  wrote on last edited by
                  #8

                  you can change the seeds just use the overloaded random construction function like this :

                  Random rand1 = new Random(1);
                  Random rand2 = new Random(2);

                  Console.WriteLine(rand1.Next(50));
                  Console.WriteLine(rand2.Next(50));

                  output is : 12 38

                  sometimes 0 can be 1

                  H 1 Reply Last reply
                  0
                  • L leppie

                    You clearly did not listen to what I said. Now go through the docs and look for seed, and the constructor for the Random class.

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)

                    H Offline
                    H Offline
                    humayunlalzad
                    wrote on last edited by
                    #9

                    Hey guys why does everyone act so pricy and rude. If I have to go through all that then why the hell do we have this forum. Now tell me how do I plant this seed of yours.

                    D 1 Reply Last reply
                    0
                    • E erfi

                      you can change the seeds just use the overloaded random construction function like this :

                      Random rand1 = new Random(1);
                      Random rand2 = new Random(2);

                      Console.WriteLine(rand1.Next(50));
                      Console.WriteLine(rand2.Next(50));

                      output is : 12 38

                      sometimes 0 can be 1

                      H Offline
                      H Offline
                      humayunlalzad
                      wrote on last edited by
                      #10

                      Thanx a lot that helped.

                      H 1 Reply Last reply
                      0
                      • H humayunlalzad

                        Hey guys why does everyone act so pricy and rude. If I have to go through all that then why the hell do we have this forum. Now tell me how do I plant this seed of yours.

                        D Offline
                        D Offline
                        DaveyM69
                        wrote on last edited by
                        #11

                        This[^] 2 page article explains why you're getting the same forboth and the solution is on page 2.

                        humayunlalzad wrote:

                        why does everyone act so pricy and rude. If I have to go through all that then why the hell do we have this forum.

                        Because this forum isn't meant to be a substitute for searching MSDN or exploring the internet. It's here to help when those have failed, otherwise it'd be a load of pointless and unstructured repetition.

                        Dave

                        H 1 Reply Last reply
                        0
                        • D DaveyM69

                          This[^] 2 page article explains why you're getting the same forboth and the solution is on page 2.

                          humayunlalzad wrote:

                          why does everyone act so pricy and rude. If I have to go through all that then why the hell do we have this forum.

                          Because this forum isn't meant to be a substitute for searching MSDN or exploring the internet. It's here to help when those have failed, otherwise it'd be a load of pointless and unstructured repetition.

                          Dave

                          H Offline
                          H Offline
                          humayunlalzad
                          wrote on last edited by
                          #12

                          You are wrong Dave. If I have to search, I can even find God. Forums are for sharing your knowledge, so that the other person, does not go through time consuming searches. Instead of going through all these advices you should do what erfi did, be a lil more specific about the answer.

                          C 1 Reply Last reply
                          0
                          • H humayunlalzad

                            Thanx a lot that helped.

                            H Offline
                            H Offline
                            hammerstein05
                            wrote on last edited by
                            #13

                            I think you need to adjust your attitude towards how forums are used. No forum is a "I'm too lazy, you should give me the answer" area. If you can't find something through searching and doing the hardwork for yourself, then you can come to a forum and ask others for their help. If you are using the Random function, the assumption is that you at least read how to use it in the first place instead of copying the functionality from another source. If you're given a clue as to what to look for, you should go and look that up and read up on it.

                            H 1 Reply Last reply
                            0
                            • H hammerstein05

                              I think you need to adjust your attitude towards how forums are used. No forum is a "I'm too lazy, you should give me the answer" area. If you can't find something through searching and doing the hardwork for yourself, then you can come to a forum and ask others for their help. If you are using the Random function, the assumption is that you at least read how to use it in the first place instead of copying the functionality from another source. If you're given a clue as to what to look for, you should go and look that up and read up on it.

                              H Offline
                              H Offline
                              humayunlalzad
                              wrote on last edited by
                              #14

                              Thanx hammer, that sounds reasonable. I will do that next time.

                              1 Reply Last reply
                              0
                              • L leppie

                                You clearly did not listen to what I said. Now go through the docs and look for seed, and the constructor for the Random class.

                                xacc.ide - now with TabsToSpaces support
                                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                                H Offline
                                H Offline
                                humayunlalzad
                                wrote on last edited by
                                #15

                                leppie I am sorry for being rude. I should have followed your clue. But I guess I was feeling lazy.

                                L C 2 Replies Last reply
                                0
                                • H humayunlalzad

                                  leppie I am sorry for being rude. I should have followed your clue. But I guess I was feeling lazy.

                                  L Offline
                                  L Offline
                                  leppie
                                  wrote on last edited by
                                  #16

                                  No problem ;P

                                  xacc.ide - now with TabsToSpaces support
                                  IronScheme - 1.0 alpha 4a out now (29 May 2008)

                                  1 Reply Last reply
                                  0
                                  • H humayunlalzad

                                    This is the piece of code

                                    Random rnd = new Random();
                                    Random rnd2 = new Random();
                                    for (int i = 0; i < 5; i++)
                                    {
                                    myIntegerCollection.Add(i);
                                    myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd2.Next(50))));
                                    }

                                    both the Random instances rnd and rnd2 are generating the same numbers over and over again, and I dont understand why.

                                    P Offline
                                    P Offline
                                    Paul Conrad
                                    wrote on last edited by
                                    #17

                                    You need to give the random number generator a different seed value each time you run the code.

                                    "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                                    1 Reply Last reply
                                    0
                                    • H humayunlalzad

                                      You are wrong Dave. If I have to search, I can even find God. Forums are for sharing your knowledge, so that the other person, does not go through time consuming searches. Instead of going through all these advices you should do what erfi did, be a lil more specific about the answer.

                                      C Offline
                                      C Offline
                                      Colin Angus Mackay
                                      wrote on last edited by
                                      #18

                                      humayunlalzad wrote:

                                      You are wrong Dave.

                                      Actually, Dave is right. Forums are for helping out when you when the documentation isn't sufficient or you don't know where to look in the documentation. It is not a replacement for using a search engine or reading documentation however much you might want it to be. You got an answer which gave you enough information to read the documentation. When you read the documentation you will get a lot of other information along with the answer which will give you greater understanding of that area. Now, take some time to calm down and understand what you are being told and think about why you are being told that. People here want to help. But, we are only prepared to help those who help themselves. It took me a while to understand that, and once I did it started to open up so many doors to me.

                                      Recent blog posts: * Event Organisation (Feedback) * LINQ to XML (part 4) * Scottish Developers June Newsletter My Blog

                                      H 1 Reply Last reply
                                      0
                                      • H humayunlalzad

                                        leppie I am sorry for being rude. I should have followed your clue. But I guess I was feeling lazy.

                                        C Offline
                                        C Offline
                                        Colin Angus Mackay
                                        wrote on last edited by
                                        #19

                                        I guess I should read the whole thread before jumping and stirring things up again after they've been sorted for 4 hours.

                                        Recent blog posts: * Event Organisation (Feedback) * LINQ to XML (part 4) * Scottish Developers June Newsletter My Blog

                                        L 1 Reply Last reply
                                        0
                                        • H humayunlalzad

                                          This is the piece of code

                                          Random rnd = new Random();
                                          Random rnd2 = new Random();
                                          for (int i = 0; i < 5; i++)
                                          {
                                          myIntegerCollection.Add(i);
                                          myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd2.Next(50))));
                                          }

                                          both the Random instances rnd and rnd2 are generating the same numbers over and over again, and I dont understand why.

                                          R Offline
                                          R Offline
                                          Robert C Cartaino
                                          wrote on last edited by
                                          #20

                                          I don't think you gain anything by having two separate random number generators. Your .add() call should probably read:

                                          myEmployeeCollection.Add(new Employee(100 + i,rnd.Next(50),("name"+ rnd.Next(50))));
                                          

                                          ... using only one instance of the Random() class. Random number generators use some formula to generate a sequence numbers that provide the appearance of randomness with a good distribution (i.e. pseudorandom). As you found out, a random number generator starting with the same seed will always produce the same sequence of numbers. Even with different seeds, there is a maximum length before the sequence begins to repeat (yes, it is a very long time but...). If you run two instances of the same formula with different entry points (seeds) in parallel, there is always a possibility that they are running sufficiently close in the sequence that the distribution between the two sets of numbers is no longer sufficiently random. Why mess with it? Just use one random number generator. Robert C. Cartaino

                                          H 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