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. Other Discussions
  3. The Weird and The Wonderful
  4. Needed a series of random numbers

Needed a series of random numbers

Scheduled Pinned Locked Moved The Weird and The Wonderful
loungeworkspace
15 Posts 9 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.
  • B Offline
    B Offline
    Bert Mitton
    wrote on last edited by
    #1

    I needed a series of random numbers, so I used the following code:

    Dim t As Integer = Environment.TickCount
    Dim n(9) As Double
    For i As Integer = 0 To 9
    Dim r As New Random(t)
    n(i) = r.Next
    Next

    You would not believe the facepalm I gave myself when I saw what I did wrong. :doh:

    N B R L L 6 Replies Last reply
    0
    • B Bert Mitton

      I needed a series of random numbers, so I used the following code:

      Dim t As Integer = Environment.TickCount
      Dim n(9) As Double
      For i As Integer = 0 To 9
      Dim r As New Random(t)
      n(i) = r.Next
      Next

      You would not believe the facepalm I gave myself when I saw what I did wrong. :doh:

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      I'm sure you know the err, but some other points: 0. If you need random, use at least two unrelated sources to seed the sequence. I use a combo of ticks from the clock and position of the mouse. 1. Within an application only ever instantiate a single randomiser and do it as the first step of the run.


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      B M 2 Replies Last reply
      0
      • N Nagy Vilmos

        I'm sure you know the err, but some other points: 0. If you need random, use at least two unrelated sources to seed the sequence. I use a combo of ticks from the clock and position of the mouse. 1. Within an application only ever instantiate a single randomiser and do it as the first step of the run.


        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

        B Offline
        B Offline
        Bert Mitton
        wrote on last edited by
        #3

        I know, but there are just some times, especially when constantly interrupted. I usually get one or two of these a week, where I just look at something I did while trying to juggle too many things, and go WTF? Something basic, that I learned in high school, and I just space out or something. At least it always provides some comic relief later. And I usually need it :)

        1 Reply Last reply
        0
        • B Bert Mitton

          I needed a series of random numbers, so I used the following code:

          Dim t As Integer = Environment.TickCount
          Dim n(9) As Double
          For i As Integer = 0 To 9
          Dim r As New Random(t)
          n(i) = r.Next
          Next

          You would not believe the facepalm I gave myself when I saw what I did wrong. :doh:

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          Are all the numbers the same? At least that would make it a lot easier to find.

          1 Reply Last reply
          0
          • B Bert Mitton

            I needed a series of random numbers, so I used the following code:

            Dim t As Integer = Environment.TickCount
            Dim n(9) As Double
            For i As Integer = 0 To 9
            Dim r As New Random(t)
            n(i) = r.Next
            Next

            You would not believe the facepalm I gave myself when I saw what I did wrong. :doh:

            R Offline
            R Offline
            RCoate
            wrote on last edited by
            #5

            I usually just use this[^] when I need random numbers.

            A A 2 Replies Last reply
            0
            • R RCoate

              I usually just use this[^] when I need random numbers.

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

              Do you work for Sony, by any chance?

              1 Reply Last reply
              0
              • R RCoate

                I usually just use this[^] when I need random numbers.

                A Offline
                A Offline
                Allan Thomas
                wrote on last edited by
                #7

                That was my first thought when I saw the post. The only difference is that he needs to roll the dice 9 times :laugh:.

                1 Reply Last reply
                0
                • B Bert Mitton

                  I needed a series of random numbers, so I used the following code:

                  Dim t As Integer = Environment.TickCount
                  Dim n(9) As Double
                  For i As Integer = 0 To 9
                  Dim r As New Random(t)
                  n(i) = r.Next
                  Next

                  You would not believe the facepalm I gave myself when I saw what I did wrong. :doh:

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Don't be too hard on yourself. Most people never realize what the 'pseudo' in 'pseudo random number generator' is about. Just turn things around and it may even come in handy at times. Do you remember the old game Elite? The game had about five galaxies with 256 star systems each. Each system had its own coordinates, a description of the planet and inhabitants and individual prices and stocks for all goods. A good old C64 would have to sacrifice a good part of its tiny memory to store all that. I'm quite sure they did not. For example, you might generate a hashcode from the coordinates of a system. Then you initialize a random number generator with this hash code and use it to randomly generate the parameters of that system. Since the hashcode will always be the same for that system, the result will be the same every time and can be recreated at will. This completely eliminates the need to store any of the static information. Just the things which are variable, like the current stocks, need to be stored. For a 'small' game I'm working on I have made a star map with 4 billiion universes with up to 4 billion galaxies, each with 4 billion x 4 billion x 4 billion coordinate points. Each coordinate point has a chance to contain a solar system, each with one or more stars of different types, a variable number of planets of many types and the planets may also have moons. Memory or storage costs: The XML configuration files to work out a convincing star system.

                  "Dark the dark side is. Very dark..." - Yoda ---
                  "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                  modified on Wednesday, September 14, 2011 9:43 AM

                  B 1 Reply Last reply
                  0
                  • L Lost User

                    Don't be too hard on yourself. Most people never realize what the 'pseudo' in 'pseudo random number generator' is about. Just turn things around and it may even come in handy at times. Do you remember the old game Elite? The game had about five galaxies with 256 star systems each. Each system had its own coordinates, a description of the planet and inhabitants and individual prices and stocks for all goods. A good old C64 would have to sacrifice a good part of its tiny memory to store all that. I'm quite sure they did not. For example, you might generate a hashcode from the coordinates of a system. Then you initialize a random number generator with this hash code and use it to randomly generate the parameters of that system. Since the hashcode will always be the same for that system, the result will be the same every time and can be recreated at will. This completely eliminates the need to store any of the static information. Just the things which are variable, like the current stocks, need to be stored. For a 'small' game I'm working on I have made a star map with 4 billiion universes with up to 4 billion galaxies, each with 4 billion x 4 billion x 4 billion coordinate points. Each coordinate point has a chance to contain a solar system, each with one or more stars of different types, a variable number of planets of many types and the planets may also have moons. Memory or storage costs: The XML configuration files to work out a convincing star system.

                    "Dark the dark side is. Very dark..." - Yoda ---
                    "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                    modified on Wednesday, September 14, 2011 9:43 AM

                    B Offline
                    B Offline
                    Bert Mitton
                    wrote on last edited by
                    #9

                    The whole point was I know better... it's just sometimes I get ahead of myself when I type, and then when I see what I did I'm going "WTF?". Sometimes, you just know better, and do it anyway. :)

                    L 1 Reply Last reply
                    0
                    • B Bert Mitton

                      The whole point was I know better... it's just sometimes I get ahead of myself when I type, and then when I see what I did I'm going "WTF?". Sometimes, you just know better, and do it anyway. :)

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Sure, but then I would not really see it as something to be ashamed about. Such things do happen. Real code horrors come from people who don't know better and don't think twice about what they did. Many of them simply are inexperienced and will learn in time.

                      "Dark the dark side is. Very dark..." - Yoda ---
                      "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                      B 1 Reply Last reply
                      0
                      • L Lost User

                        Sure, but then I would not really see it as something to be ashamed about. Such things do happen. Real code horrors come from people who don't know better and don't think twice about what they did. Many of them simply are inexperienced and will learn in time.

                        "Dark the dark side is. Very dark..." - Yoda ---
                        "Shut up, Yoda, and just make yourself another toast." - Obi Wan Kenobi

                        B Offline
                        B Offline
                        Bert Mitton
                        wrote on last edited by
                        #11

                        That may be true, but there are days when you can identify me by the palmprint on my forehead. :laugh:

                        1 Reply Last reply
                        0
                        • B Bert Mitton

                          I needed a series of random numbers, so I used the following code:

                          Dim t As Integer = Environment.TickCount
                          Dim n(9) As Double
                          For i As Integer = 0 To 9
                          Dim r As New Random(t)
                          n(i) = r.Next
                          Next

                          You would not believe the facepalm I gave myself when I saw what I did wrong. :doh:

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Its quite rare that you get a post of "Look what I did", in this forum. Have a 5 :)

                          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                          B 1 Reply Last reply
                          0
                          • L Lost User

                            Its quite rare that you get a post of "Look what I did", in this forum. Have a 5 :)

                            Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                            B Offline
                            B Offline
                            Bert Mitton
                            wrote on last edited by
                            #13

                            I screw up as much as anyone... I just consider it a plus that I notice when I do. Of course, if I don't notice a mistake, did I really screw up? I'm going with no. :)

                            1 Reply Last reply
                            0
                            • N Nagy Vilmos

                              I'm sure you know the err, but some other points: 0. If you need random, use at least two unrelated sources to seed the sequence. I use a combo of ticks from the clock and position of the mouse. 1. Within an application only ever instantiate a single randomiser and do it as the first step of the run.


                              Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                              M Offline
                              M Offline
                              Matty22
                              wrote on last edited by
                              #14

                              Why bother passing in a custom seed if you don't need one anyhow? The default constructor of random seeds itself appropriately

                              1 Reply Last reply
                              0
                              • B Bert Mitton

                                I needed a series of random numbers, so I used the following code:

                                Dim t As Integer = Environment.TickCount
                                Dim n(9) As Double
                                For i As Integer = 0 To 9
                                Dim r As New Random(t)
                                n(i) = r.Next
                                Next

                                You would not believe the facepalm I gave myself when I saw what I did wrong. :doh:

                                L Offline
                                L Offline
                                Luis D Aguilar
                                wrote on last edited by
                                #15

                                I think the error was naming the variables as "n", "t" or "i". ;P

                                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