Click here to Skip to main content
15,888,260 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.

 
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 
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 <a href="https://tc39.es/ecma262/#sec-math.random">https://tc39.es/ecma262/#sec-math.random</a> 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 | :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 your loop is never reached and you get an infinite loop.

If you have no choice of data type, check the documentation and use a number close to the resolution limit of that data type. As an example, if the data type has 20 bit resolution for the significant digits then the maximum integer value it can hold is (2e+20)-1 .

Or take the "hands on" approach and make a loop in which you increment a counter and check when the number is the same as the last.

Just my random opinion Big Grin | :-D
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 
GeneralRe: Cryptic Error Time: Favorite Part of Your Day Pin
Richard Deeming7-Apr-20 1:03
mveRichard Deeming7-Apr-20 1:03 
GeneralRe: Cryptic Error Time: Favorite Part of Your Day Pin
raddevus7-Apr-20 2:21
mvaraddevus7-Apr-20 2:21 
GeneralGriff and 10gb update Pin
Afzaal Ahmad Zeeshan29-Mar-20 12:22
professionalAfzaal Ahmad Zeeshan29-Mar-20 12:22 
GeneralRe: Griff and 10gb update Pin
Super Lloyd29-Mar-20 12:58
Super Lloyd29-Mar-20 12:58 
GeneralRe: Griff and 10gb update Pin
Afzaal Ahmad Zeeshan29-Mar-20 21:51
professionalAfzaal Ahmad Zeeshan29-Mar-20 21:51 
GeneralRe: Griff and 10gb update Pin
OriginalGriff29-Mar-20 22:36
mveOriginalGriff29-Mar-20 22:36 
RantWhen Poor Design Gets Baked Into a File Format Pin
Kevin Li (Li, Ken-un)29-Mar-20 6:30
Kevin Li (Li, Ken-un)29-Mar-20 6:30 
GeneralRe: When Poor Design Gets Baked Into a File Format Pin
phil.o29-Mar-20 6:45
professionalphil.o29-Mar-20 6:45 
GeneralRe: When Poor Design Gets Baked Into a File Format Pin
Super Lloyd29-Mar-20 13:48
Super Lloyd29-Mar-20 13:48 
GeneralRe: When Poor Design Gets Baked Into a File Format Pin
honey the codewitch29-Mar-20 7:28
mvahoney the codewitch29-Mar-20 7:28 
GeneralRe: When Poor Design Gets Baked Into a File Format Pin
Greg Utas29-Mar-20 8:24
professionalGreg Utas29-Mar-20 8:24 

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.