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


Groups > comp.lang.javascript > #40020 > unrolled thread

How bad is Javascripts randomisation function?

Started byJonas Thörnvall <jonas.thornvall@gmail.com>
First post2021-04-28 16:11 -0700
Last post2021-05-29 12:04 +0000
Articles 7 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  How bad is Javascripts randomisation function? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-04-28 16:11 -0700
    Re: How bad is Javascripts randomisation function? JJ <jj4public@gmail.com> - 2021-04-29 16:03 +0700
    Re: How bad is Javascripts randomisation function? Arno Welzel <usenet@arnowelzel.de> - 2021-04-29 20:24 +0200
      Re: How bad is Javascripts randomisation function? John Stockton <dr.j.r.stockton@gmail.com> - 2021-05-23 08:57 -0700
        Re: How bad is Javascripts randomisation function? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-05-24 10:22 -0700
          Re: How bad is Javascripts randomisation function? John Stockton <dr.j.r.stockton@gmail.com> - 2021-05-29 04:37 -0700
            Re: How bad is Javascripts randomisation function? Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-05-29 12:04 +0000

#40020 — How bad is Javascripts randomisation function?

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2021-04-28 16:11 -0700
SubjectHow bad is Javascripts randomisation function?
Message-ID<4166bb6e-0388-4ab1-ba1f-919ed718c43bn@googlegroups.com>
<HTML>
<HEAD>
</HEAD>
<BODY>
<button onClick=init();>Play a game</button><p>

Roll reset "serie", mean last result<br>
<textarea id="rollSerie" name="rollSerie" rows="20" cols="80">
</textarea>
</BODY>
<script>
totRolls=0;
totResult=0;
resets=0;
totVal=0;
textOut="";

function init(){
	document.getElementById("rollSerie").value="";
	textOut="";
	selectStarter();
	playGame();
}


function selectStarter(){
	bitME=Math.round(Math.random()) ;
	if(bitME==0){starter="Champion";} else {starter="Challenger";}
	document.getElementById("rollSerie").value+=starter+" STARTS\n";
}

function playGame(){
	champScore=0;chalScore=0;
	gameEnd=false;
	while(chalScore<100 && champScore<100){
		if(starter=="Champion"){
			champion();
			if(champScore>99){winScore=champScore;winner="Champion";break;}
			challenger();
			if(chalScore>99){winScore=chalScore;winner="Challenger";break;}
		} else {
			challenger();
			if(chalScore>99){winScore=chalScore;winner="Challenger";break;}
			champion();
			if(champScore>99){winScore=champScore;winner="Champion";break;}
		}

	} 
	document.getElementById("rollSerie").value+=textOut;	
	document.getElementById("rollSerie").value+="Winner "+winner+" "+winScore;
}

function challenger(){
	chalRolls=0;
	chalVal=0;
	textOut+="\nChallenger SERIE ";
	while(chalVal<21){
		//Think this random shitty do more randomness
		rollVal=Math.floor(Math.random() * 6) + 1;
		textOut+=rollVal+" ";
		if (rollVal==1){
			break;
		} else {
			chalVal=chalVal+rollVal;
			chalRolls++;
		}
	}
	chalScore=chalVal+chalScore;
	textOut+="\nChallenger rolls "+chalRolls+"\n";
	textOut+="Challenger value "+chalVal+"\n";
	textOut+="Challenger Total "+chalScore+"\n\n";
}


function champion(){
	champRolls=0;
	champVal=0;
	textOut+="\nChampion SERIE ";
	while(champVal<21){
		//Think this random shitty do more randomness
		rollVal=Math.floor(Math.random() * 6) + 1;
		textOut+=rollVal+" ";
		if (rollVal==1){
			
		} else {
			champVal=champVal+rollVal;
			champRolls++;
		}
	}
	champScore=champVal+champScore;
	textOut+="\nChampion rolls "+champRolls+"\n";
	textOut+="Champion value "+champVal+"\n";
	textOut+="Champion Total "+champScore+"\n\n";
}


</script>
</HTML>

[toc] | [next] | [standalone]


#40022

FromJJ <jj4public@gmail.com>
Date2021-04-29 16:03 +0700
Message-ID<1bbmgikfc1doq.1j5n8uisar4l6$.dlg@40tude.net>
In reply to#40020
On Wed, 28 Apr 2021 16:11:31 -0700 (PDT), Jonas Thörnvall wrote:
[snip]
> 	while(chalVal<21){
> 		//Think this random shitty do more randomness
> 		rollVal=Math.floor(Math.random() * 6) + 1;
> 		textOut+=rollVal+" ";
> 		if (rollVal==1){
> 			break;
> 		} else {
> 			chalVal=chalVal+rollVal;
> 			chalRolls++;
> 		}
> 	}
[snip]

Just a coincident. Make a delay at each loop, or underclock the CPU, to see 
the difference.

[toc] | [prev] | [next] | [standalone]


#40023

FromArno Welzel <usenet@arnowelzel.de>
Date2021-04-29 20:24 +0200
Message-ID<if0bv9FbvtuU4@mid.individual.net>
In reply to#40020
Jonas Thörnvall:

[snip]

Why not just this:

<https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/random>


-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#40101

FromJohn Stockton <dr.j.r.stockton@gmail.com>
Date2021-05-23 08:57 -0700
Message-ID<9a5a922c-df27-4e3d-81a6-45d2eae4c958n@googlegroups.com>
In reply to#40023
On Thursday, 29 April 2021 at 19:24:49 UTC+1, Arno Welzel wrote:
> Jonas Thörnvall: 
> 
> [snip] 
> 
> Why not just this: 
> 
> <https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/random> 


Quite a lot bad, as I recall.

See
 http://web.archive.org/web/20150510110440/http://www.merlyn.demon.co.uk/js-randm.htm
which links to
 http://web.archive.org/web/20180611034551/https://github.com/nquinlan/better-random-numbers-for-javascript-mirror .
See also http://www.jibbering.com/faq/index.html#randomNumber
and see what https://en.wikipedia.org/wiki/Random_number says and cites.

According to
http://web.archive.org/web/20130127232731/http://www.merlyn.demon.co.uk/quotings.htm -
https://en.wikipedia.org/wiki/Donald_Knuth wrote/said
 "Random numbers should not be generated with a method chosen at random".


-- 
  (c) John Stockton, near London, UK.  Using Google Groups.           |
 Mail: J.R.""""""""@physics.org - or as Reply-To, if any.             |

[toc] | [prev] | [next] | [standalone]


#40142

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2021-05-24 10:22 -0700
Message-ID<1d7b7897-f8ed-4bb5-9263-58789eef717en@googlegroups.com>
In reply to#40101
On Sunday, May 23, 2021 at 10:57:14 AM UTC-5, John Stockton wrote:
> On Thursday, 29 April 2021 at 19:24:49 UTC+1, Arno Welzel wrote: 
> > Jonas Thörnvall: 
> > 
> > [snip] 
> > 
> > Why not just this: 
> > 
> > <https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/random>
> Quite a lot bad, as I recall. 
> 
> See 
> http://web.archive.org/web/20150510110440/http://www.merlyn.demon.co.uk/js-randm.htm 
> which links to 
> http://web.archive.org/web/20180611034551/https://github.com/nquinlan/better-random-numbers-for-javascript-mirror . 
> See also http://www.jibbering.com/faq/index.html#randomNumber 
> and see what https://en.wikipedia.org/wiki/Random_number says and cites. 
> 
> According to 
> http://web.archive.org/web/20130127232731/http://www.merlyn.demon.co.uk/quotings.htm - 
> https://en.wikipedia.org/wiki/Donald_Knuth wrote/said 
> "Random numbers should not be generated with a method chosen at random". 

If you want better random numbers use the following:

<https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues>

[toc] | [prev] | [next] | [standalone]


#40145

FromJohn Stockton <dr.j.r.stockton@gmail.com>
Date2021-05-29 04:37 -0700
Message-ID<d970269c-d96f-47d5-a634-02df85b550a1n@googlegroups.com>
In reply to#40142
On Monday, 24 May 2021 at 18:22:22 UTC+1, Michael Haufe (TNO) wrote:
> On Sunday, May 23, 2021 at 10:57:14 AM UTC-5, John Stockton wrote: 
> > On Thursday, 29 April 2021 at 19:24:49 UTC+1, Arno Welzel wrote: 
> > > Jonas Thörnvall: 
> > > 
> > > [snip] 
> > > 
> > > Why not just this: 
> > > 
> > > <https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/random> 
> > Quite a lot bad, as I recall. 
> > 
> > See 
> > http://web.archive.org/web/20150510110440/http://www.merlyn.demon.co.uk/js-randm.htm 
> > which links to 
> > http://web.archive.org/web/20180611034551/https://github.com/nquinlan/better-random-numbers-for-javascript-mirror . 
> > See also http://www.jibbering.com/faq/index.html#randomNumber 
> > and see what https://en.wikipedia.org/wiki/Random_number says and cites. 
> > 
> > According to 
> > http://web.archive.org/web/20130127232731/http://www.merlyn.demon.co.uk/quotings.htm - 
> > https://en.wikipedia.org/wiki/Donald_Knuth wrote/said 
> > "Random numbers should not be generated with a method chosen at random".
> If you want better random numbers use the following: 
> 
> <https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues>


That link says "The Crypto.getRandomValues() method lets you get cryptographically strong random values.".  That sentence implies that random values exist which are not cryptographically strong, and so are not given.  Therefore, the method does not give values in the range randomly.  It may suit Jonas; but it does not provide full randomness, and is badly named.

Theorem : Any group producing things and giving them new names needs to include a hard-core PEDANT who is experienced not only in things of that nature but also in all languages, natural and artificial, which may be significantly involved. // Remember the story (probably true) of the luxury English car which sold unexpectedly badly in Germany - because its name included Rot13("Zvfg").

I would prefer to trust random code such as by Johannes Baagøe and associates/rivals; see for example his "Math.random"  and the following discussion - https://groups.google.com/g/comp.lang.javascript/c/u8oCu7eNvUI/m/775xVCV_aAsJ?pli=1 (CLJ, Jun 2009).

AFAIR, those are better than https://en.wikipedia.org/wiki/Lehmer_random_number_generator ; but the latter or its modifications should I think usually suffice for non-crypto work.


-- 
  (c) John Stockton, near London, UK.  Using Google Groups.           |
 Mail: J.R.""""""""@physics.org - or as Reply-To, if any.             |

[toc] | [prev] | [next] | [standalone]


#40146

FromJon Ribbens <jon+usenet@unequivocal.eu>
Date2021-05-29 12:04 +0000
Message-ID<slrnsb4beu.1q1s.jon+usenet@raven.unequivocal.eu>
In reply to#40145
On 2021-05-29, John Stockton <dr.j.r.stockton@gmail.com> wrote:
> That link says "The Crypto.getRandomValues() method lets you get
> cryptographically strong random values.".  That sentence implies that
> random values exist which are not cryptographically strong, and so are
> not given.  Therefore, the method does not give values in the range
> randomly.  It may suit Jonas; but it does not provide full randomness,
> and is badly named.

This paragraph is so confused it's difficult to know where to start.
It seems like you think that some numbers are more random than others
and getRandomValues() avoids returning the "less random" numbers.

"Cryptographically-strong random number generators" are a subset of
"random number generators" (for example, excluding those methods that
produce low-quality randomness) so your statement above is like saying
"Fido is not a mammal because he is a dog". 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web