Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #34221

Re: Random numbers

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Random numbers
Date 2017-10-28 18:54 +0200
Organization PointedEars Software (PES)
Message-ID <2010509.iZASKD2KPV@PointedEars.de> (permalink)
References (1 earlier) <ose4is$14l2$1@gioia.aioe.org> <19e8d664-5c4b-4118-99de-5133a2317841@googlegroups.com> <osipj9$lfm$1@gioia.aioe.org> <random-20171022231003@ram.dialup.fu-berlin.de> <ot1sdu$1qkk$1@gioia.aioe.org>

Show all headers | View raw


emf wrote:
^^^
It is considered polite here to post using one’s real name.

> function random(top) {
>      var now '= new Date();

Syntax error, see below.

>      var tim = now.getTime();
>      var 4rhdm;

Unused variable.  Use JSHint: <https://en.wikipedia.org/wiki/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 :->

<https://en.wikipedia.org/wiki/Modulo_operation>
<https://en.wikipedia.org/wiki/Congruence_relation>
 
> […]
> -- '

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: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Random numbers emf <emfril@gmail.com> - 2017-10-19 07:35 -0400
  Re: Random numbers Julio Di Egidio <julio@diegidio.name> - 2017-10-19 05:03 -0700
  Re: Random numbers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-19 21:06 +0200
    Re: Random numbers "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-19 12:10 -0700
      Re: Random numbers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-19 22:04 +0200
        Re: Random numbers "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-19 13:41 -0700
          Measuring time (was: Random numbers) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-19 22:58 +0200
            Re: Measuring time (was: Random numbers) Dr J R Stockton <J.R.Stockton@physics.org> - 2017-10-24 13:05 -0700
              Re: Measuring time Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-25 00:59 +0200
                Re: Measuring time Dr J R Stockton <J.R.Stockton@physics.org> - 2017-10-25 13:36 -0700
                Re: Measuring time Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-26 19:21 +0200
              Re: Measuring time (was: Random numbers) Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2017-10-26 10:45 +0200
                Re: Measuring time (was: Random numbers) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-10-26 12:27 +0200
                Re: Measuring time (was: Random numbers) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-26 19:04 +0200
                Re: Measuring time (was: Random numbers) Hans-Georg Michna <hans-georg@michna.com> - 2017-10-28 11:05 +0200
                Re: Measuring time (was: Random numbers) Dr J R Stockton <J.R.Stockton@physics.org> - 2017-10-29 03:03 -0700
                Re: Measuring time (was: Random numbers) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-29 17:56 +0100
                Re: Measuring time (was: Random numbers) dr.j.r.stockton@gmail.com - 2017-10-29 15:12 -0700
                Re: Measuring time Sam E <why.should.this@be.email.invalid> - 2017-10-29 17:35 -0500
                Re: Measuring time (was: Random numbers) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-30 13:00 +0100
                Re: Measuring time (was: Random numbers) Tim Slattery <tim@risingdove.com> - 2017-10-26 14:13 -0400
  Re: Random numbers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-10-19 22:21 +0100
    Re: Random numbers Julio Di Egidio <julio@diegidio.name> - 2017-10-19 22:25 -0700
      Re: Random numbers "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2017-10-20 21:52 -0700
        Re: Random numbers Julio Di Egidio <julio@diegidio.name> - 2017-10-20 22:40 -0700
          Re: Random numbers "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2017-10-21 12:40 -0700
            Re: Random numbers Julio Di Egidio <julio@diegidio.name> - 2017-10-21 12:57 -0700
              Re: Random numbers Julio Di Egidio <julio@diegidio.name> - 2017-10-21 12:59 -0700
                Re: Random numbers "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2017-10-22 16:37 -0700
                Re: Random numbers Julio Di Egidio <julio@diegidio.name> - 2017-10-22 17:23 -0700
  Re: Random numbers emf <emfril@gmail.com> - 2017-10-20 20:32 -0400
    Re: Random numbers "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-10-21 09:47 -0700
      Re: Random numbers emf <emfril@gmail.com> - 2017-10-22 14:55 -0400
        Re: Random numbers emf <emfril@gmail.com> - 2017-10-27 22:23 -0400
          Re: Random numbers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-10-28 18:54 +0200

csiph-web