Math.Random in function always the same result - javascript

I have this code:
function raffle(){
number = Math.random(100) * 100;}
raffle();
But everytime I raffle(); the number is the same.

Math.random() returns an a random number between 0 (inclusive) and 1 (exclusive). The Javascript random function doesn't take any parameters.
If you want a random number x such that 0 ≤ x < 100, then you would do:
function raffle() {
return Math.random() * 100;
}

Your raffle function never returns a value.
Here's a version which returns the random value.
function raffle() {
return Math.random() * 100;
}
(The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.)

You can simply add the iteration number to every results, like so:
let runNumber = 0
function raffle(){
return Math.random() * 100 + runNumber++;
}
raffle();
You probably run it one after another, which is where the "pseudo" random part kick in.

Related

Order of operation and How does the random() play in this scenario

Just started learning loops and I'm having trouble understanding the order of operation here in the let value, along with how the random() works in this scenario.
From what it looks like: Math.floor() prevents decimals and Math.random() selects a random number between 0 and 36. Does random() select a random value for both MAX and MIN? Does random() also generate a random for its self to be multiplied by whatever the value of MAX and MIN equal after being subtracted, then adding the MIN back?
const MIN = 0;
const MAX = 36;
var testNumber = 15;
var i = 1;
while (MAX) {
let randomValue = Math.floor(Math.random() * (MAX - MIN)) + MIN;
if (randomValue == testNumber) {
break;
}
Math.random() provides a random floating point number between 0 and 1. If you want to get a wider range of random values, you multiply by the magnitude of the range you want (i.e. MAX - MIN). Then, if MIN is great than 0 you'll need to add it to the resulting random number, otherwise the results range would be 0 up to (MAX - MIN).
As you say, Math.floor() simply rounds the result down to the nearest integer.
The Math.floor() function returns the largest integer less than or equal to a given number. This is in contrast to the Math.ceil() function that returns the largest integer more than or equal to a given number.
The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with an approximately uniform distribution over that range — which you can then scale to your desired range.
So in the case of your randomValue variable, a pseudo-random value between the values for MIN and MAX is generated. This value could have decimals because of how Math.random() operates thus Math.floor is used to get a whole number. MIN is added to the end so that the random value will always fall within the range, especially if MIN is not 0.

Correct function using Math.random() to get 50/50 chance

Which is the correct function for getting a precise 50/50 chance:
return Math.random() < 0.5;
Vs
return Math.random() <= 0.5;
Math.random():
The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1); that is, from 0 (inclusive) up to but not including 1 (exclusive)
The random number is either in the range [0,0.5) or [0.5,1). So you should use return Math.random() < 0.5; to have a (theoretical) 50/50 chance.
The first one is the correct because the random number generators returns a number from 0 to 0.99999999 (depends on the exact accuracy of the generator itself)
So by splitting the values into two groups using the "<" operator, you should get two equal ranges:
[0 upto 0.49999999] and [0.5 upto 0.9999999]

Javascript: Math.random

If num parameter is 52, how many possible return values are there? is it 52 or 53? If I understand this correctly, Math.random uses random values from 0 to 1 inclusive. If so, then 0 is a possible return value and so is 52. This results in 53 possible return values. Is this correct? Reason I ask is that a book that I'm learning from uses this code for a deck of cards. I wonder if num should equal 51 ?
Thanks ...
function getRandom(num) {
var my_num = Math.floor(Math.random * num);
return my_num;
};
Math.floor(Math.random() * num) // note random() is a function.
This will return all integers from 0 (including 0) to num (NOT including num).
Math.random returns a number between 0 (inclusive) and 1 (exclusive). Multiplying the result by X gives you between 0 (inclusive) and X (exclusive). Adding or subtracting X shifts the range by +-X.
Here's some handy functions from MDN:
// Returns a random number between 0 (inclusive) and 1 (exclusive)
function getRandom() {
return Math.random();
}
// Returns a random number between min and max
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Since Math.random returns a real number between [0,1) (1 is not inclusive), multiplying the result returns a real number between [0, 52).
Since you are flooring the result, the maximum number returned is 51 and there are 52 distinct values (counting 0).
Since value of Math.random varies from 0 to 1(exclusive);
so if you pass 52 in getRandom, return value will vary from 0 to 52(exclusive). so getRandom can return only 52 values. as you are using Math.floor. the max value can be returned is 51.

Need an explanation of this javascript

I have a question about this script I found and used. It works but I don't get why. The exercise was to make a list with random numbers from -50 to 50. The function below uses Math.floor(Math.random() * (the part i dont understand).
If I put this calculation on google I got as answer 151 and Math.random()*151 does not do from -50 to 50.
Can someone give me a clear explanation about this function below because I am sure that I am missing something.
this script works but I only want a clear explanation how
for (i = 0; i <= 100; i++)
{
Rnumber[i] = randomFromTo(-50,50);
}
function randomFromTo(from, to)
{
return Math.floor(Math.random() * (to - from + 1) + from);
}
to - from + 1 = 50 - (-50) + 1 = 101
Math.random() * 101 = number in range [0,101[
Math.floor([0,101[) = integer in range [0,100]
[0,100] + from = [0,100] + (-50) = integer in range [-50,50]
Which is exactly what is asked for.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random
Math.random returns a floating-point, pseudo-random number in the
range [0, 1) that is, from 0 (inclusive) up to but not including 1
(exclusive), which you can then scale to your desired range.
which when multiplied with a number > 1 and floored gives you an integer
Math.random() - get only value between 0 and 1.
Math.floor( number ) get integer down rounded value from number.
You should:
function randomFromTo(from, to)
{
// you can use doubled bitwise NOT operator which also as Math.floor get integer value from number but is much faster.
// ~1 == -2 , ~-2 == 1 and ~1.5 == -2 :)
return ~~( --from + ( Math.random() * ( ++to - from )) )
}

Random number with fixed length

I want to generate a random integer number with 0-9 numbers and with length = 5. I try this:
function genRand(min,max) {
for (var i = 1; i <= 5; i++) {
var range = max - min + 1;
return Math.floor(Math.random()*range) + min;
}
}
and call:
genRand(0,9);
But it always returns 1 number, not 5 (
Help please!
function genRand() {
return Math.floor(Math.random()*89999+10000);
}
return exits the function on the first loop.
The smallest 5 digit number is 10000, the largest is 99999, or 10000+89999.
Return a random number between 0 and 89999, and add it to the minimum.
var ran5=10000+Math.round(Math.floor()*90000)
Math.floor rounds down, and Math.random is greater than or equal to 0 and less than 1.
Here's a more generalized version of Nate B's answer:
function rand(digits) {
return Math.floor(Math.random()*parseInt('8' + '9'.repeat(digits-1))+parseInt('1' + '0'.repeat(digits-1)));
}
To get a 5 digit random number generate random numbers between the range (10000, 99999). Or generate randomly 5 single digits and paste them.
EDIT
The process you have shown simply will generate one number and return to the caller. The think which might work is (pseudo code) :
int sum = 0;
int m = 1;
for (i=0;i<5;i++)
{
sum = sum + m * random (0, 9);
/* or */
sum = sum * m + random (0, 9);
m = m * 10;
}
Or better generate 5 digit random numbers with rand (10000, 99999)

Categories