Reputation: 39
I have this code:
function raffle(){
number = Math.random(100) * 100;}
raffle();
But everytime I raffle(); the number is the same.
Upvotes: 0
Views: 2639
Reputation: 9640
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.
Upvotes: 0
Reputation: 1515
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.)
Upvotes: 0
Reputation: 1583
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;
}
Upvotes: 3