Click here to Skip to main content
15,888,158 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
AnswerRe: What's the Most Concise, Human-Understandable Practical Language? Pin
johnywhy22-Oct-21 8:07
johnywhy22-Oct-21 8:07 
Generalhtml button not firing on mobile, oh Pin
raddevus11-May-20 9:16
mvaraddevus11-May-20 9:16 
GeneralRe: html button not firing on mobile, oh Pin
Richard Deeming11-May-20 9:31
mveRichard Deeming11-May-20 9:31 
GeneralRe: html button not firing on mobile, oh Pin
raddevus11-May-20 9:39
mvaraddevus11-May-20 9:39 
GeneralRe: html button not firing on mobile, oh Pin
raddevus11-May-20 10:54
mvaraddevus11-May-20 10:54 
GeneralRe: html button not firing on mobile, oh Pin
Marc Clifton11-May-20 9:32
mvaMarc Clifton11-May-20 9:32 
GeneralRe: html button not firing on mobile, oh Pin
raddevus11-May-20 9:41
mvaraddevus11-May-20 9:41 
GeneralGenerated over 5.5 Billion random numbers, but no 0. Pin
raddevus19-Apr-20 7:15
mvaraddevus19-Apr-20 7:15 
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 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.

JavaScript
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 exposes the idea that probabilistically (is that a real word? Roll eyes | :rolleyes: ) you will never hit a specific value.
That SO will also lead you to other readings like : How does JavaScript’s Math.random() generate random numbers? | Hacker Noon[^]
Since 0 is a specific value, it is unlikely that you will ever see that value.

Here's The Thought I Had To Get To For Understanding
Basically think about if you stuck your hand into a bin of numbers which contained from 0 to 1.7976931348623157e+308 (largest Number in JavaScript)*. How likely would it be that you get 0? Or, how many times would you have to stick your hand into the bin to randomly grab the 0? Lots.

But, at some point you must see that value, but you may wait a long time (hundreds of years or something?)
Or it could happen with the first Math.random() call you make.

I think this is an interesting thought experiment, because it will make you think. Smile | :)

*It is some number similar to this for the number of decimal values 0 <= N < 1 (dependent upon Number data type size and precision).

modified 19-Apr-20 21:51pm.

GeneralRe: Generated over 5.5 Billion random numbers, but no 0. *UPDATED* Pin
Peter_in_278019-Apr-20 12:13
professionalPeter_in_278019-Apr-20 12:13 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
Rick York19-Apr-20 12:14
mveRick York19-Apr-20 12:14 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
raddevus19-Apr-20 15:47
mvaraddevus19-Apr-20 15:47 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
Nelviticus26-Apr-20 23:12
Nelviticus26-Apr-20 23:12 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
PhilipOakley26-Apr-20 23:34
professionalPhilipOakley26-Apr-20 23:34 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
BryanFazekas27-Apr-20 1:10
BryanFazekas27-Apr-20 1:10 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
raddevus27-Apr-20 3:20
mvaraddevus27-Apr-20 3:20 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
raddevus27-Apr-20 3:32
mvaraddevus27-Apr-20 3:32 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
raddevus27-Apr-20 5:59
mvaraddevus27-Apr-20 5:59 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
BryanFazekas27-Apr-20 7:18
BryanFazekas27-Apr-20 7:18 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
ElectronProgrammer29-Apr-20 9:32
ElectronProgrammer29-Apr-20 9:32 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
raddevus29-Apr-20 10:53
mvaraddevus29-Apr-20 10:53 
GeneralRe: Generated over 5.5 Billion random numbers, but no 0. Pin
ElectronProgrammer29-Apr-20 12:17
ElectronProgrammer29-Apr-20 12:17 
GeneralCryptic Error Time: Favorite Part of Your Day Pin
raddevus6-Apr-20 9:57
mvaraddevus6-Apr-20 9:57 
GeneralRe: Cryptic Error Time: Favorite Part of Your Day Pin
Greg Utas6-Apr-20 10:08
professionalGreg Utas6-Apr-20 10:08 
GeneralRe: Cryptic Error Time: Favorite Part of Your Day Pin
raddevus6-Apr-20 10:09
mvaraddevus6-Apr-20 10:09 
GeneralRe: Cryptic Error Time: Favorite Part of Your Day Pin
Slow Eddie8-Apr-20 2:56
professionalSlow Eddie8-Apr-20 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.