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. Generated over 5.5 Billion random numbers, but no 0.

Generated over 5.5 Billion random numbers, but no 0.

Scheduled Pinned Locked Moved The Weird and The Wonderful
javascriptcsscomdata-structurestools
14 Posts 7 Posters 59 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.
  • raddevusR Offline
    raddevusR Offline
    raddevus
    wrote on last edited by
    #1

    EDIT - NOTE: someone pointed out that I said 5.51 billion but I actually generated 551 Billion random numbers. Yesterday, I was thinking about the JavaScript Math.random() function.

    The MDN explains[^]

    The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range

    I had a script that I uses Math.random() to generate values in range of 1 - 10 (inclusive). I basically ignored the fact that you could ever get zero. Then something happened and I started wondering why I never seemed to hit the 0 value. Generate As Many Random Values As Possible So I decided to write a script and let it just generate random values and run a long while to see if I'd ever actually get 0. I let the following script run (via NodeJS) for something like 5 or 6 hours and it never generated a value of 0. I finally just killed the script.

    var counter = 1;
    var MaxLoops = Number.MAX_VALUE;
    // note MaxLoops = 1.7976931348623157e+308

    function runForZero(){
    console.log("running...");
    for (var x=1;x<=MaxLoops;x++)
    {
    var rnd = Math.random();
    if (rnd === 0)
    {
    // if the random value generated ever equals 0, then give message and exit.
    console.log("rnd is " + rnd + " It took " + counter + " tries.");
    return;
    }
    if (counter % 5000000 == 0){
    // For every 5 Million random numbers generated, output so I can tell it's running
    console.log(new Date().toTimeString() + " - Still running : " + counter);
    }
    counter++;
    }
    console.log("Complete.");
    // 04-18-2020 - generated 551,855,000,000 random numbers without generating a zero
    }

    After Reading a Few Thoughts, I Understand, but.... I read this javascript - Can Math.random() exactly equal .5 - Stack Overflow[^] which really expo

    P R N P B 6 Replies Last reply
    0
    • raddevusR raddevus

      EDIT - NOTE: someone pointed out that I said 5.51 billion but I actually generated 551 Billion random numbers. Yesterday, I was thinking about the JavaScript Math.random() function.

      The MDN explains[^]

      The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range

      I had a script that I uses Math.random() to generate values in range of 1 - 10 (inclusive). I basically ignored the fact that you could ever get zero. Then something happened and I started wondering why I never seemed to hit the 0 value. Generate As Many Random Values As Possible So I decided to write a script and let it just generate random values and run a long while to see if I'd ever actually get 0. I let the following script run (via NodeJS) for something like 5 or 6 hours and it never generated a value of 0. I finally just killed the script.

      var counter = 1;
      var MaxLoops = Number.MAX_VALUE;
      // note MaxLoops = 1.7976931348623157e+308

      function runForZero(){
      console.log("running...");
      for (var x=1;x<=MaxLoops;x++)
      {
      var rnd = Math.random();
      if (rnd === 0)
      {
      // if the random value generated ever equals 0, then give message and exit.
      console.log("rnd is " + rnd + " It took " + counter + " tries.");
      return;
      }
      if (counter % 5000000 == 0){
      // For every 5 Million random numbers generated, output so I can tell it's running
      console.log(new Date().toTimeString() + " - Still running : " + counter);
      }
      counter++;
      }
      console.log("Complete.");
      // 04-18-2020 - generated 551,855,000,000 random numbers without generating a zero
      }

      After Reading a Few Thoughts, I Understand, but.... I read this javascript - Can Math.random() exactly equal .5 - Stack Overflow[^] which really expo

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      Quick thumbnail calculation: Most optimistic (for this purpose) and very likely case, the PRNG returns one of 2^52 values. It might be more, but that's how many bits are in the mantissa of a JS Number. 2^52 is about 4.5e15. You tried 5.5e9 5.5e11 samples, so your chance of hitting any specific value is on the order of 1e-6 1e-4. Your observation still is entirely plausible. ;P If you ran it for, say, 3 years you'd be getting on for likely to hit zero.(P ~ 0.5) Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      1 Reply Last reply
      0
      • raddevusR raddevus

        EDIT - NOTE: someone pointed out that I said 5.51 billion but I actually generated 551 Billion random numbers. Yesterday, I was thinking about the JavaScript Math.random() function.

        The MDN explains[^]

        The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range

        I had a script that I uses Math.random() to generate values in range of 1 - 10 (inclusive). I basically ignored the fact that you could ever get zero. Then something happened and I started wondering why I never seemed to hit the 0 value. Generate As Many Random Values As Possible So I decided to write a script and let it just generate random values and run a long while to see if I'd ever actually get 0. I let the following script run (via NodeJS) for something like 5 or 6 hours and it never generated a value of 0. I finally just killed the script.

        var counter = 1;
        var MaxLoops = Number.MAX_VALUE;
        // note MaxLoops = 1.7976931348623157e+308

        function runForZero(){
        console.log("running...");
        for (var x=1;x<=MaxLoops;x++)
        {
        var rnd = Math.random();
        if (rnd === 0)
        {
        // if the random value generated ever equals 0, then give message and exit.
        console.log("rnd is " + rnd + " It took " + counter + " tries.");
        return;
        }
        if (counter % 5000000 == 0){
        // For every 5 Million random numbers generated, output so I can tell it's running
        console.log(new Date().toTimeString() + " - Still running : " + counter);
        }
        counter++;
        }
        console.log("Complete.");
        // 04-18-2020 - generated 551,855,000,000 random numbers without generating a zero
        }

        After Reading a Few Thoughts, I Understand, but.... I read this javascript - Can Math.random() exactly equal .5 - Stack Overflow[^] which really expo

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        You have a slight disagreement with yourself. 551,855,000,000 is not 5.5 billion. ;)

        "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

        raddevusR 1 Reply Last reply
        0
        • R Rick York

          You have a slight disagreement with yourself. 551,855,000,000 is not 5.5 billion. ;)

          "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

          raddevusR Offline
          raddevusR Offline
          raddevus
          wrote on last edited by
          #4

          Haha, you are right. And it was actually the bigger of the two numbers... It was 551 billion. I was attempting not to exaggerate in my headline (seriously) and then I put the wrong number down. :laugh:

          1 Reply Last reply
          0
          • raddevusR raddevus

            EDIT - NOTE: someone pointed out that I said 5.51 billion but I actually generated 551 Billion random numbers. Yesterday, I was thinking about the JavaScript Math.random() function.

            The MDN explains[^]

            The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range

            I had a script that I uses Math.random() to generate values in range of 1 - 10 (inclusive). I basically ignored the fact that you could ever get zero. Then something happened and I started wondering why I never seemed to hit the 0 value. Generate As Many Random Values As Possible So I decided to write a script and let it just generate random values and run a long while to see if I'd ever actually get 0. I let the following script run (via NodeJS) for something like 5 or 6 hours and it never generated a value of 0. I finally just killed the script.

            var counter = 1;
            var MaxLoops = Number.MAX_VALUE;
            // note MaxLoops = 1.7976931348623157e+308

            function runForZero(){
            console.log("running...");
            for (var x=1;x<=MaxLoops;x++)
            {
            var rnd = Math.random();
            if (rnd === 0)
            {
            // if the random value generated ever equals 0, then give message and exit.
            console.log("rnd is " + rnd + " It took " + counter + " tries.");
            return;
            }
            if (counter % 5000000 == 0){
            // For every 5 Million random numbers generated, output so I can tell it's running
            console.log(new Date().toTimeString() + " - Still running : " + counter);
            }
            counter++;
            }
            console.log("Complete.");
            // 04-18-2020 - generated 551,855,000,000 random numbers without generating a zero
            }

            After Reading a Few Thoughts, I Understand, but.... I read this javascript - Can Math.random() exactly equal .5 - Stack Overflow[^] which really expo

            N Offline
            N Offline
            Nelviticus
            wrote on last edited by
            #5

            If you toss a coin, how many attempts will it take until it comes up heads? I'd plump for somewhere between one and infinity (inclusive). The concept is the same, no matter the size of the pool of possible values.

            Regards Nelviticus

            1 Reply Last reply
            0
            • raddevusR raddevus

              EDIT - NOTE: someone pointed out that I said 5.51 billion but I actually generated 551 Billion random numbers. Yesterday, I was thinking about the JavaScript Math.random() function.

              The MDN explains[^]

              The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range

              I had a script that I uses Math.random() to generate values in range of 1 - 10 (inclusive). I basically ignored the fact that you could ever get zero. Then something happened and I started wondering why I never seemed to hit the 0 value. Generate As Many Random Values As Possible So I decided to write a script and let it just generate random values and run a long while to see if I'd ever actually get 0. I let the following script run (via NodeJS) for something like 5 or 6 hours and it never generated a value of 0. I finally just killed the script.

              var counter = 1;
              var MaxLoops = Number.MAX_VALUE;
              // note MaxLoops = 1.7976931348623157e+308

              function runForZero(){
              console.log("running...");
              for (var x=1;x<=MaxLoops;x++)
              {
              var rnd = Math.random();
              if (rnd === 0)
              {
              // if the random value generated ever equals 0, then give message and exit.
              console.log("rnd is " + rnd + " It took " + counter + " tries.");
              return;
              }
              if (counter % 5000000 == 0){
              // For every 5 Million random numbers generated, output so I can tell it's running
              console.log(new Date().toTimeString() + " - Still running : " + counter);
              }
              counter++;
              }
              console.log("Complete.");
              // 04-18-2020 - generated 551,855,000,000 random numbers without generating a zero
              }

              After Reading a Few Thoughts, I Understand, but.... I read this javascript - Can Math.random() exactly equal .5 - Stack Overflow[^] which really expo

              P Offline
              P Offline
              PhilipOakley
              wrote on last edited by
              #6

              That's not random enough! You'll need a bigger bit size to get close to say simulating a shuffled deck of cards (52!). I.e. chance that a shuffled deck comes out 'sorted'. The (linear) size of the universe, in Planck lengths, still isn't enough to even simulate the random shuffles of a card deck (leads to arguments about it being a predictable universe - don't go there;-). (Size of a random card deck: Every second re-arrange the deck to a new sequence, every billion years, step 1cm along the equator, every time you pass the Pacific, empty out a 5mL teaspoon, each time the Pacific empties, place an A4 sheet of 80gsm paper on a pile, until it reaches the moon. Visit moon a million times. Almost done. Phew, ... need more Jokers)

              1 Reply Last reply
              0
              • raddevusR raddevus

                EDIT - NOTE: someone pointed out that I said 5.51 billion but I actually generated 551 Billion random numbers. Yesterday, I was thinking about the JavaScript Math.random() function.

                The MDN explains[^]

                The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range

                I had a script that I uses Math.random() to generate values in range of 1 - 10 (inclusive). I basically ignored the fact that you could ever get zero. Then something happened and I started wondering why I never seemed to hit the 0 value. Generate As Many Random Values As Possible So I decided to write a script and let it just generate random values and run a long while to see if I'd ever actually get 0. I let the following script run (via NodeJS) for something like 5 or 6 hours and it never generated a value of 0. I finally just killed the script.

                var counter = 1;
                var MaxLoops = Number.MAX_VALUE;
                // note MaxLoops = 1.7976931348623157e+308

                function runForZero(){
                console.log("running...");
                for (var x=1;x<=MaxLoops;x++)
                {
                var rnd = Math.random();
                if (rnd === 0)
                {
                // if the random value generated ever equals 0, then give message and exit.
                console.log("rnd is " + rnd + " It took " + counter + " tries.");
                return;
                }
                if (counter % 5000000 == 0){
                // For every 5 Million random numbers generated, output so I can tell it's running
                console.log(new Date().toTimeString() + " - Still running : " + counter);
                }
                counter++;
                }
                console.log("Complete.");
                // 04-18-2020 - generated 551,855,000,000 random numbers without generating a zero
                }

                After Reading a Few Thoughts, I Understand, but.... I read this javascript - Can Math.random() exactly equal .5 - Stack Overflow[^] which really expo

                B Offline
                B Offline
                BryanFazekas
                wrote on last edited by
                #7

                Run the test again, except instead of if (rnd === 0) use if (rnd < 0.000001) Or some small number. That will give you an idea of how close to zero you're getting.

                raddevusR 3 Replies Last reply
                0
                • B BryanFazekas

                  Run the test again, except instead of if (rnd === 0) use if (rnd < 0.000001) Or some small number. That will give you an idea of how close to zero you're getting.

                  raddevusR Offline
                  raddevusR Offline
                  raddevus
                  wrote on last edited by
                  #8

                  BryanFazekas wrote:

                  use if (rnd < 0.000001)

                  :thumbsup: That could be very interesting. I'll try this later and we'll see what we get.

                  1 Reply Last reply
                  0
                  • B BryanFazekas

                    Run the test again, except instead of if (rnd === 0) use if (rnd < 0.000001) Or some small number. That will give you an idea of how close to zero you're getting.

                    raddevusR Offline
                    raddevusR Offline
                    raddevus
                    wrote on last edited by
                    #9

                    I thought about this a bit too. And, remember, it is randomly grabbing a number out of a "bucket" which is a huge set of numbers. It is no more likely to choose one that is < 0.000001 than any other range : considering this to be the range of 0 - 0.000001. The point here is that yes, I will get more hits for this because it is a range and not just a single random value (0) that I'm attempting to get, but it doesn't really indicate much more than that. Just some thoughts.

                    1 Reply Last reply
                    0
                    • B BryanFazekas

                      Run the test again, except instead of if (rnd === 0) use if (rnd < 0.000001) Or some small number. That will give you an idea of how close to zero you're getting.

                      raddevusR Offline
                      raddevusR Offline
                      raddevus
                      wrote on last edited by
                      #10

                      FYI - I tried it your suggestion and ran it 10 million times (only takes about 1 or 2 seconds to run). We got 7 values < 0.000001 out of 10 million generated random numbers. running...

                      rnd is 8.534479201127709e-7 in 2432341 tries - lessThanCounter: 1
                      rnd is 2.3682786065570838e-7 in 3184756 tries - lessThanCounter: 2
                      rnd is 9.513474119593468e-7 in 4548719 tries - lessThanCounter: 3
                      11:54:49 GMT-0400 (EDT) - Still running : 5000000
                      rnd is 3.635436747195797e-7 in 7235129 tries - lessThanCounter: 4
                      rnd is 9.258161568492795e-7 in 7321777 tries - lessThanCounter: 5
                      rnd is 3.1373473086127035e-7 in 7743862 tries - lessThanCounter: 6
                      rnd is 3.704604489840335e-7 in 8554983 tries - lessThanCounter: 7

                      Again, it makes sense that because we now used a range of values that the probability of hitting a value is higher and we do get the values. However, again, if we chose any specific value then we'd possibly wait forever for that value to be hit. Or, it could occur the first time. It definitely gets you thinking. :)

                      B 1 Reply Last reply
                      0
                      • raddevusR raddevus

                        FYI - I tried it your suggestion and ran it 10 million times (only takes about 1 or 2 seconds to run). We got 7 values < 0.000001 out of 10 million generated random numbers. running...

                        rnd is 8.534479201127709e-7 in 2432341 tries - lessThanCounter: 1
                        rnd is 2.3682786065570838e-7 in 3184756 tries - lessThanCounter: 2
                        rnd is 9.513474119593468e-7 in 4548719 tries - lessThanCounter: 3
                        11:54:49 GMT-0400 (EDT) - Still running : 5000000
                        rnd is 3.635436747195797e-7 in 7235129 tries - lessThanCounter: 4
                        rnd is 9.258161568492795e-7 in 7321777 tries - lessThanCounter: 5
                        rnd is 3.1373473086127035e-7 in 7743862 tries - lessThanCounter: 6
                        rnd is 3.704604489840335e-7 in 8554983 tries - lessThanCounter: 7

                        Again, it makes sense that because we now used a range of values that the probability of hitting a value is higher and we do get the values. However, again, if we chose any specific value then we'd possibly wait forever for that value to be hit. Or, it could occur the first time. It definitely gets you thinking. :)

                        B Offline
                        B Offline
                        BryanFazekas
                        wrote on last edited by
                        #11

                        Yes, I expect that we can choose any number and get the same result (no hits) as you got for zero. I had my testing hat on -- I was wondering if the RND function is truly random, or if boundaries get missed? Your results indicate we are getting really low numbers. Curiosity satisfied!

                        1 Reply Last reply
                        0
                        • raddevusR raddevus

                          EDIT - NOTE: someone pointed out that I said 5.51 billion but I actually generated 551 Billion random numbers. Yesterday, I was thinking about the JavaScript Math.random() function.

                          The MDN explains[^]

                          The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range

                          I had a script that I uses Math.random() to generate values in range of 1 - 10 (inclusive). I basically ignored the fact that you could ever get zero. Then something happened and I started wondering why I never seemed to hit the 0 value. Generate As Many Random Values As Possible So I decided to write a script and let it just generate random values and run a long while to see if I'd ever actually get 0. I let the following script run (via NodeJS) for something like 5 or 6 hours and it never generated a value of 0. I finally just killed the script.

                          var counter = 1;
                          var MaxLoops = Number.MAX_VALUE;
                          // note MaxLoops = 1.7976931348623157e+308

                          function runForZero(){
                          console.log("running...");
                          for (var x=1;x<=MaxLoops;x++)
                          {
                          var rnd = Math.random();
                          if (rnd === 0)
                          {
                          // if the random value generated ever equals 0, then give message and exit.
                          console.log("rnd is " + rnd + " It took " + counter + " tries.");
                          return;
                          }
                          if (counter % 5000000 == 0){
                          // For every 5 Million random numbers generated, output so I can tell it's running
                          console.log(new Date().toTimeString() + " - Still running : " + counter);
                          }
                          counter++;
                          }
                          console.log("Complete.");
                          // 04-18-2020 - generated 551,855,000,000 random numbers without generating a zero
                          }

                          After Reading a Few Thoughts, I Understand, but.... I read this javascript - Can Math.random() exactly equal .5 - Stack Overflow[^] which really expo

                          E Offline
                          E Offline
                          ElectronProgrammer
                          wrote on last edited by
                          #12

                          I do not know javascript but, if it works similarly to c/c++, there is a potential problem with your program that you need to be aware and I do not think anyone pointed it out yet. The same documentation you link points out that "The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user". and the documentation at https://tc39.es/ecma262/#sec-math.random states that "Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls." Depending on how the sequence of numbers is generated by the underlying (maybe system) function, you might get a fixed (although large) number of different numbers and, when you reach the end of the sequence, it just restarts the same sequence. If the seed can not be reset or changed like it is mentioned in the documentation, you will never get a specific number if that number is not part of the sequence. That is the main reason why cautioned c/c++ programmers reseed their random number generation functions every once in a while inside their main program loop. You can easily test if the sequence is repeating by storing the first, say one thousand, numbers and test each time you get a new number if it matches the first number drawn. After the first number being the same, the next drawn number must equal the second and so on. If you get the same initial sequence, it is clearly repeating since it is statistically improbable that the same (large) sequence of numbers repeats itself in a true random sequence of numbers. I already had this problem in the past in c++ and, in one of the tests I made, I got a sequence of just five different numbers on a 32bit platform :wtf: . This is probably the reason why in the documentation is suggested to use another random number function for cryptography. Just one note about your program. Do not use floating point numbers as increment counters in loops. They might not work as expected due to the bit limit of your platform (probably 64 bit). Imagine your counter is already at 1.2345678911234455445656765345542e+300 and you increment one. If your counter has already reached its maximum resolution, incrementing by one does nothing and you get 1.2345678911234455445656765345542e+300 instead of the expected 1.2345678911234455445656765345543e+300 (which would also be wrong since you do not have 300 digits). This means that the terminating condition of

                          raddevusR 1 Reply Last reply
                          0
                          • E ElectronProgrammer

                            I do not know javascript but, if it works similarly to c/c++, there is a potential problem with your program that you need to be aware and I do not think anyone pointed it out yet. The same documentation you link points out that "The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user". and the documentation at https://tc39.es/ecma262/#sec-math.random states that "Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls." Depending on how the sequence of numbers is generated by the underlying (maybe system) function, you might get a fixed (although large) number of different numbers and, when you reach the end of the sequence, it just restarts the same sequence. If the seed can not be reset or changed like it is mentioned in the documentation, you will never get a specific number if that number is not part of the sequence. That is the main reason why cautioned c/c++ programmers reseed their random number generation functions every once in a while inside their main program loop. You can easily test if the sequence is repeating by storing the first, say one thousand, numbers and test each time you get a new number if it matches the first number drawn. After the first number being the same, the next drawn number must equal the second and so on. If you get the same initial sequence, it is clearly repeating since it is statistically improbable that the same (large) sequence of numbers repeats itself in a true random sequence of numbers. I already had this problem in the past in c++ and, in one of the tests I made, I got a sequence of just five different numbers on a 32bit platform :wtf: . This is probably the reason why in the documentation is suggested to use another random number function for cryptography. Just one note about your program. Do not use floating point numbers as increment counters in loops. They might not work as expected due to the bit limit of your platform (probably 64 bit). Imagine your counter is already at 1.2345678911234455445656765345542e+300 and you increment one. If your counter has already reached its maximum resolution, incrementing by one does nothing and you get 1.2345678911234455445656765345542e+300 instead of the expected 1.2345678911234455445656765345543e+300 (which would also be wrong since you do not have 300 digits). This means that the terminating condition of

                            raddevusR Offline
                            raddevusR Offline
                            raddevus
                            wrote on last edited by
                            #13

                            Thanks for posting, it was an interesting read. Yeah, I know about the issues requiring seed. The JavaScript implementation of Math.random() handles that itself and doesn't allow us to control it from the outside. Math.random() is definitely manipulated and is not a strong PRNG. Definitely not a CSPRNG[^]. I was curious only about one thing: Why hadn't I seen Math.random() ever generate a value of zero (even after running for days) since the documentation states that the value returned is 0 <= r < 1. JavaScript handles types for the dev and Number is the only choice. It is floating point and I was basically attempting to create a boundless loop when I set my loop watcher to Number.MAX_VALUE (1.7976931348623157e+308). It was just a way to get it to run for hours, days, months. :) Good discussion.

                            E 1 Reply Last reply
                            0
                            • raddevusR raddevus

                              Thanks for posting, it was an interesting read. Yeah, I know about the issues requiring seed. The JavaScript implementation of Math.random() handles that itself and doesn't allow us to control it from the outside. Math.random() is definitely manipulated and is not a strong PRNG. Definitely not a CSPRNG[^]. I was curious only about one thing: Why hadn't I seen Math.random() ever generate a value of zero (even after running for days) since the documentation states that the value returned is 0 <= r < 1. JavaScript handles types for the dev and Number is the only choice. It is floating point and I was basically attempting to create a boundless loop when I set my loop watcher to Number.MAX_VALUE (1.7976931348623157e+308). It was just a way to get it to run for hours, days, months. :) Good discussion.

                              E Offline
                              E Offline
                              ElectronProgrammer
                              wrote on last edited by
                              #14

                              The first time I used random numbers in C, in my first programming class in university, I went through the same questions just to realize that certain numbers are simply not in the sequence generated by the pseudo-random number generator. In your example it is zero but you can probably pick any other number and the same will happen. If javascript does not automatically reseed you will have always the same sequence and never have the number you want. Later, in university, I studied how pseudo random number generators were constructed in silicon and understood why certain numbers never show up using a certain seed. If I recall correctly (as it was two decades ago), the seed must be one that triggers the maximum length sequence of the generator and most seeds do not. If you search for "pseudo random generator logic gates" or something similar you probably find information about it if you are interested. Curiously, I trashed yesterday one of the Springer publications from 2007 that shows up in the results :doh: I am sorry if I sounded too aggressive about the floating point counter. Usually a "while(1)" is used for boundless loops. When I saw it I thought you were using it without knowing. Unfortunately, I already had my share of people "asking" me to fix their programs that do not stop because of loop conditions that are never met (like using integers with double/boolean/string/pointers/etc). Best regards and keep the curiosity high :)

                              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