2

I want to random a numbers like this from -5....0....5.

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<script>
function myFunction() {
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 7-1);
}
</script>

</body>
</html>

In this case it returns only -1. It is possible to do in javascript math object?

Yas Smitch
  • 269
  • 2
  • 13
  • [Generate random number between two numbers in JavaScript](https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript) – Roland Starke Dec 07 '17 at 08:19
  • Just to add another method `Math.floor(Math.random() * 6) - Math.floor(Math.random() * 6)` – 11thdimension Dec 07 '17 at 08:34

3 Answers3

3

You could use a factor of 11 (this returns a value of 0 ... 10) and adjust the number by -5.

console.log(Math.floor(Math.random() * 11) - 5);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

Try this may this works (There you can give a range also to the random number)...

<button onclick="getRandomInt(-5,5)">Try it</button>
<p id="demo"></p>

<script>
function getRandomInt(min, max) {
    document.getElementById("demo").innerHTML = Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>

REF Link

A.D.
  • 2,352
  • 2
  • 15
  • 25
0

In this case it returns only -1. It is possible to do in javascript math object?

You need to use brackets (), in your case it resulted in Math.floor(Math.random() * 7-1 ) => Math.floor(0.xxx-1 ) => Math.floor(-0.xxx ) => -1

Make it

Math.floor(Math.random() * (7-1) );

I want to random a numbers like this from -5....0....5.

First get a number between 0 and 5

Math.floor(Math.random() * (5-0) );

Then multiply the same by either 1 or -1 randomly

var value = Math.floor(Math.random() * (5-0) );
var sign = window.performance.now() % 2 == 1 ? 1 : -1; //get the current timestamp of the system in nanoseconds (something like 1512635632715) and checking if that number is even or odd
value *= sign ; 

Added Demo with nano seconds instead of milliseconds

function generateRandom() {
  var value = Math.floor(Math.random() * (5 - 0));
  var sign = window.performance.now() % 2 == 1 ? 1 : -1; 
  value *= sign;
  return value;
}

var arr = [];
for( var counter = 0; counter < 100; counter++)
{
   arr.push(generateRandom());
}
console.log(arr);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • can you explain this? : var sign = new Date().getTime() % 2 == 1 ? 1 : -1; – Yas Smitch Dec 07 '17 at 08:33
  • It's not random. If called 1000 times in one second, it will return number of same sign for all 1000 times. – 11thdimension Dec 07 '17 at 08:34
  • @11thdimension how? you meant 1000 times in one millisecond? – gurvinder372 Dec 07 '17 at 08:34
  • @YadYoung added the explanation. – gurvinder372 Dec 07 '17 at 08:34
  • Yes, I meant millisecond. – 11thdimension Dec 07 '17 at 08:35
  • @11thdimension So how is that a problem? Do you really envisage a random number generator being called so many times within a millisecond? Also only sign value is millisecond based, not the entire value. **Randomness still exists even without sign even though sign generation is reasonably random enough**. – gurvinder372 Dec 07 '17 at 08:37
  • It's an incorrect answer as simple as that. I can imagine a situation where it will be called multiple times in a millisecond. In fact it will be true for any call in a normal iteration. A biased randomness is not randomness. – 11thdimension Dec 07 '17 at 08:41
  • @11thdimension *It's an incorrect answer as simple as that* I don't see how! *I can imagine a situation where it will be called multiple times in a millisecond* Doesn't matter - randomness still exists. – gurvinder372 Dec 07 '17 at 08:43