Path: csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.38.237.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Random numbers Date: Sat, 28 Oct 2017 18:54:32 +0200 Organization: PointedEars Software (PES) Lines: 109 Message-ID: <2010509.iZASKD2KPV@PointedEars.de> References: <19e8d664-5c4b-4118-99de-5133a2317841@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="38.237.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.237.38"; logging-data="31761"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-User-ID: U2FsdGVkX19bdhm0rVVTcOwopbDmPgYSzB2W/PQWwWx9ZR2WJmAoMg== Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== Cancel-Lock: sha1:H4PYa9O39QxkXBKfAIRM3YljwCM= X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. function random(top) { > var now '= new Date(); Syntax error, see below. > var tim = now.getTime(); > var 4rhdm; Unused variable. Use JSHint: p. > if (top != 0) { > rndm = (tim % top) + 1; > } else { > rndm = 0; // garbage in, garbage out > } > return rndm; > } > > […] > I do not know how Math.random() gets its pseudorandom results, Presumably (the equivalent of) /dev/random. > but I am sure this function's results should be better. You are wrong. *Dead* wrong. And the fact that you did not realize it does not bode well for your being able to create a good PRNG. > What do you think about using centiseconds? Even greater nonsense, see below. Using the modulo operation (“%”) on a *predictably* increasing numeric value like the return value of “new Date().getTime()” anywhere in a generator will get you *predictably* consecutive results belonging to *neighboring* equivalence classes in a modulus ring. So there is _nothing_ *random* about it. For example: | -> var input = Array.apply(null, {length: 10}).map(function (e, i) { | return i; | }); | -> input | <- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | | -> input.map(function (e) { return e % 3; }) | <- [0, 1, 2, 0, 1, 2, 0, 1, 2, 0] Here the modulus ring is [0] / \ [2]---[1] whereas [0] means the equivalence class to which all e’s belong for which e ≡ 0 (mod 3) holds; obviously, e ∈ {3n | n ∈ ℤ}. ¹) With your modification, it is just | -> input.map(function (e) { return (e % 3) + 1; }) | <- [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] [1] / \ [3]---[2] instead. Obviously, if the dividend of Euclidean division is much greater than the divisor (which is the modulus), or the divisor is not much less than the dividend, it gets only worse: | -> Array.apply(null, {length: 10}).map(function () { | return new Date().getTime(); | }).map(function (e) { return (e % 3) + 1; }) | [2, 2, 3, 3, 3, 3, 3, 3, 3, 3] And with centiseconds it is even worse: | -> Array.apply(null, {length: 10}).map(function () { | return Math.round(new Date().getTime() / 10); | }).map(function (e) { return (e % 3) + 1; }) | [3, 3, 3, 3, 3, 3, 3, 3, 3, 3] Very random, indeed :-> > […] > -- ' Your “'” key is borked. _____ ¹) BTW, Chrome Dev Tools appear to be able now to tell 0 and −0 apart. Enter “-0” or “-1 % 1”: the result should be “-0”, not “0”. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.