Question about JavaScript Math.random() and basic logic

Rainer Plumer

I wrote a simple piece of code to compare random array difference and found something ... that I don't quite understand.

  1. I generate 2 arrays filled with random numbers
  2. Add up the differences between the random numbers
  3. Print out the average difference

I would have expected the result to be random number close to 0.5 but in practice, it is 0.3333.

Why does the random number array home in on 0.3 and not 0.5?

const result = document.getElementById('result');
const generateRandomNrArray = (nrNumbers) => {
	let i;
	let result = [];
	for (i = 0; i < nrNumbers; i++) {
		result.push(Math.random());
	}
	return result;
}
const getArrayDiff = (arr1, arr2) => {
  var diff = 0;
  arr1.forEach(function (v1, index) {
      diff += Math.abs(v1 - arr2[index]);
  });
  return diff;
}
const run = (nr) => {
  const arr1 = generateRandomNrArray(nr);
  const arr2 = generateRandomNrArray(nr);
  const totalDiff = getArrayDiff(arr1, arr2); 
  
  result.innerHTML = "Average difference:" + (totalDiff / nr);
}
button {font-size: 2em;}
<div id="result"></div>
<button id="run" onclick="run(1500)">Click Me</button>

Mark Meyer

This basically boils down to a limit and it makes sense. Consider the combinations of numbers between 0 and 10 and count the various differences you can make.

For example, there is one combination with a difference of 9 — (0, 9). There are 5 with a difference of 5:

[0, 5],  
[1, 6], 
[2, 7], 
[3, 8], 
[4, 9]

But there are nine combinations with a difference of 1:

[1, 2], 
[2, 3], 

... 
[8, 9]

With 0 - 10 the counts are:

{1: 9, 2: 8, 3: 7, 4: 6, 5: 5, 6: 4, 7: 3, 8: 2, 9: 1}

There are 45 combinations and the average difference of the those combinations is 3.6666 not 5 because there are more smaller differences than larger ones.

When you increase the granularity from 0 - 10 to 0 - 100 the same pattern holds. There are 99 combinations that result in a difference 1 and only 50 with a difference of 50 for an average of 33.6666.

As you increase the number of significant digits the opposite directions the opposite direction with finer and finer divisions between 0 and 1, you find the same process as the limit approaches 1/3. There are many more smaller difference than larger ones pulling the average difference down. For 0-1 in 0.1 intervals you'll see 9 with a difference of 0.1 and 5 with a difference of 0.5, at 0.01 there will be 99 with a difference 0.01, and 50 with a difference of 0.5. As the interval approaches 0 the average of the differences approaches 1/3.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript Math.random() not *that* random…?

From Dev

Math & JavaScript | Random chances

From Dev

A basic question about continuous integration

From Dev

very basic question about ppa

From Dev

Javascript - Math.random as parameter

From Dev

javascript TypeError: Math.random is not a function on chrome

From Dev

Math.random and web programming in JavaScript

From Dev

Math.random() javascript function *undefined* on Chrome

From Dev

Javascript Math.random() and conditional expressions

From Dev

basic javascript image gallery logic error?

From Dev

Concept of and basic questions about separating logic (C++) and GUI (Qt)

From Dev

Generate a random math Equation using Random numbers and operators in Javascript

From Dev

Basic javascript questions about onclicks, createElement and more?

From Dev

Math logic to substitute if statement

From Dev

Game Logic Math headbreaker

From Dev

Python math logic error

From Dev

Java Math logic

From Dev

Game Logic Math headbreaker

From Dev

Logic App/JavaScript - Assigning a random password to each JSON object

From Dev

How to get unique numbers from Math.random in javascript?

From Dev

How to get random math operator for a quiz qustion in javascript

From Dev

JavaScript Math.random Normal distribution (Gaussian bell curve)?

From Dev

How to get unique numbers from Math.random in javascript?

From Dev

How to get random math operator for a quiz qustion in javascript

From Dev

Javascript: Creating an Array of random math equations as objects, 20 times

From Dev

how to repeat result from math.random in javascript?

From Dev

Why JavaScript Math.random() returns same number multiple times

From Dev

Is it possible for Math.random() === Math.random()

From Dev

Math.Random is not so random

Related Related

  1. 1

    Javascript Math.random() not *that* random…?

  2. 2

    Math & JavaScript | Random chances

  3. 3

    A basic question about continuous integration

  4. 4

    very basic question about ppa

  5. 5

    Javascript - Math.random as parameter

  6. 6

    javascript TypeError: Math.random is not a function on chrome

  7. 7

    Math.random and web programming in JavaScript

  8. 8

    Math.random() javascript function *undefined* on Chrome

  9. 9

    Javascript Math.random() and conditional expressions

  10. 10

    basic javascript image gallery logic error?

  11. 11

    Concept of and basic questions about separating logic (C++) and GUI (Qt)

  12. 12

    Generate a random math Equation using Random numbers and operators in Javascript

  13. 13

    Basic javascript questions about onclicks, createElement and more?

  14. 14

    Math logic to substitute if statement

  15. 15

    Game Logic Math headbreaker

  16. 16

    Python math logic error

  17. 17

    Java Math logic

  18. 18

    Game Logic Math headbreaker

  19. 19

    Logic App/JavaScript - Assigning a random password to each JSON object

  20. 20

    How to get unique numbers from Math.random in javascript?

  21. 21

    How to get random math operator for a quiz qustion in javascript

  22. 22

    JavaScript Math.random Normal distribution (Gaussian bell curve)?

  23. 23

    How to get unique numbers from Math.random in javascript?

  24. 24

    How to get random math operator for a quiz qustion in javascript

  25. 25

    Javascript: Creating an Array of random math equations as objects, 20 times

  26. 26

    how to repeat result from math.random in javascript?

  27. 27

    Why JavaScript Math.random() returns same number multiple times

  28. 28

    Is it possible for Math.random() === Math.random()

  29. 29

    Math.Random is not so random

HotTag

Archive