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. Help with random numbers

Help with random numbers

Scheduled Pinned Locked Moved C#
debugginghelpquestionlounge
7 Posts 4 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.
  • A Offline
    A Offline
    amatbrewer
    wrote on last edited by
    #1

    When I run the following code I get the same number 36 times, but when I step through it in debug I get 'random' numbers?!?! Any idea why? for (int a = 0; a < 36; a++) { Random generator = new Random(); int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }

    David Wilkes

    L T 2 Replies Last reply
    0
    • A amatbrewer

      When I run the following code I get the same number 36 times, but when I step through it in debug I get 'random' numbers?!?! Any idea why? for (int a = 0; a < 36; a++) { Random generator = new Random(); int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }

      David Wilkes

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      what is "list3" ?

      A 1 Reply Last reply
      0
      • L led mike

        what is "list3" ?

        A Offline
        A Offline
        amatbrewer
        wrote on last edited by
        #3

        Sorry, "list3" is a custom data type similar to a listbox.

        David Wilkes

        L 1 Reply Last reply
        0
        • A amatbrewer

          Sorry, "list3" is a custom data type similar to a listbox.

          David Wilkes

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, you create a new random generator every time; they each start with a dfeault seed value, which depends on the current time. As a result, while single stepping it seems all right; when running it finishes in less than 1 second, so all seeds are the same. You should create and use only one generator outside the loop and keep using it. :)

          Luc Pattyn [My Articles] [Forum Guidelines]

          L A 2 Replies Last reply
          0
          • L Luc Pattyn

            Hi, you create a new random generator every time; they each start with a dfeault seed value, which depends on the current time. As a result, while single stepping it seems all right; when running it finishes in less than 1 second, so all seeds are the same. You should create and use only one generator outside the loop and keep using it. :)

            Luc Pattyn [My Articles] [Forum Guidelines]

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            Luc Pattyn wrote:

            you create a new random generator every time;

            :doh: I totally missed that!

            1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, you create a new random generator every time; they each start with a dfeault seed value, which depends on the current time. As a result, while single stepping it seems all right; when running it finishes in less than 1 second, so all seeds are the same. You should create and use only one generator outside the loop and keep using it. :)

              Luc Pattyn [My Articles] [Forum Guidelines]

              A Offline
              A Offline
              amatbrewer
              wrote on last edited by
              #6

              That makes total sense. Figured it was something simple. Thanks!!!

              David Wilkes

              1 Reply Last reply
              0
              • A amatbrewer

                When I run the following code I get the same number 36 times, but when I step through it in debug I get 'random' numbers?!?! Any idea why? for (int a = 0; a < 36; a++) { Random generator = new Random(); int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }

                David Wilkes

                T Offline
                T Offline
                Thomas Stockwell
                wrote on last edited by
                #7

                Random generator = new Random(); for (int a = 0; a < 36; a++) { int randomValue; randomValue = generator.Next(1, 20); list3.Add(a, randomValue); }

                Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

                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