Math class random() method logic in JavaScript - javascript

I'm trying to understand the logic behind this method. For example, I have the following code:
function getRandomArbitrary(min, max){
return Math.random() * (max - min) + min;
}
I know that this function, when called, will call a random number that will be between the minimum and maximum value, but I don't understand how the program does this by decreasing the minimum value from the maximum, adding the minimum value again and then, multiplying the values by the random() method.

Remember order of operations here: the multiplication of Math.random() and (max - min) will occur before adding to min.
As for how the rest of it works, let's break it down with some example values.
Let's say we're calling this method like so:
getRandomArbitrary(3, 10);
We're using min=3 and max=10, which makes the rest of the formula look like so (extra parentheses added for clarity):
return (Math.random() * (10 - 3)) + 3;
// return (Math.random() * 7) + 3;
According to the docs, Math.random() will return "a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1)". By multiplying that random number by another number, we're changing the range that we're dealing with.
Our initial lower range is 0, and 0 * 7 = 0, so that stays as 0.
Our initial upper range is <1 (alllllllmost 1, but always less than it), so if then our new upper range is <1 * 7 = <7. So the value Math.random() * 7 will be a random number between 0 and <7.
Last, we're adding the min value we want to our result, which again will change our range. The lower range will now be 0 + 3 = 3 and the upper range will be <7 + 3 = <10, so now we're getting a random number bet

Related

Why is Math.floor needed to generate a random number between x and y?

Why do you need to use Math.floor() when trying to generate a random number between 2 other numbers in js? I can't seem to get it to work just like this: Math.random() * ((y-x)+1) + x)
According to the MDN docs,
The Math.random() function returns a floating-point, pseudo-random
number in the range 0–1 (inclusive of 0, but not 1).
This means that the number you will be returned is between 0 and 1. Now, having a long bunch of decimals points isn't super user friendly for us developers so we like to multiply it by a certain value, add one (since the random function return a number between 0 and 1 but excluding 1), and round down the number in order to achieve an integer that is easy to read and work with.
For example:
var x = Math.floor((Math.random() * 10) + 1);
This takes the number generated by "Math.random()", let's say that value is 0.12345, and then multiplies it by 10 which gives a result of 1.2345. We then add 1 to have a value of 2.2345. Now, all those decimal points aren't very useful to us since we want a nice round number, therefore we call the Math.floor() method which round down the value to a value of 2.
Again, the Math.floor method is called so we don't have to worry about all those decimal points and are instead given a nice round integer.
Math.random() will generate a random number between 0 (inclusive) and 1 (exclusive). This usually means that have a number with a lot of decimal places, which isn't very user-friendly. To convert it to an Integer, you will need to use Math.floor() (converts number to nearest Integer less than it) or Math.ceil() (converts number to nearest Integer larger than it).
console.log(Math.random());
var x = 10;
var y = 5;
var random;
console.log((random = Math.random() * (y-x)+1 + x));
console.log("Nearest Integer less than random: "+Math.floor(random));
To get a random integer between x (inclusive) and y (exclusive):
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
To get a random number between x and y (not necessarily an integer), just remove Math.floor():
function getRndInteger(min, max) {
return (Math.random() * (max - min)) + min;
}
Demo:
Min:
<input type="number" id="min">
<br/>
Max:
<input type="number" id="max"/><br/>
<button onClick="generateRandom()">
Generate Random Number
</button>
<p/>
<span id="result"></span>
<script>
function getRndInteger(min, max) {
return (Math.random() * (max - min)) + min;
}
function generateRandom(){
var min = parseFloat(document.getElementById('min').value);
var max = parseFloat(document.getElementById('max').value);
if(!isNaN(min)&&!isNaN(max)&&min<max){
document.getElementById('result').textContent = 'Random Number: '+getRndInteger(min, max);
} else {
document.getElementById('result').innerHTML = '<b>Please enter valid min and max values.</b>';
}
}
</script>

Javascript: Generate random number using a range inside a range

I am trying to make a number simulation that functions as follows:
Begins at baseline value
Generates random value within given range of baseline
Takes generated value and generate another value within same range
All generated values remain within a larger range
So if I have a starting value of 5, I need it to generate a number within a range of say 2. It then needs to take that value (between 3 and 7) and generate another number with the same range of 2, and so on. But I also need any numbers generated to stay between 0 and 10.
I can use random() with a single defined range, but I don't know how to do two:
function generateValue(min, max) {
var max = 2,
min = 0,
value = (Math.random() * (max - min) + min).toFixed(6);
return value;
}
You need a function that accepts 4 arguments: baseline, range, min and max (if I understand you correctly). Also, I believe you had a mistake in you formula for getting a random number from a range. Here's a function that does what you need, I think:
function generateValue(baseline, range, min, max) {
let localMin = Math.max(baseline - range, min);
let localMax = Math.min(baseline + range, max);
return (Math.random() * (localMax - localMin) + localMin).toFixed(6);
}
Here is your function. Its parameter is the baseline value. You can make min, max, range parameters too if you wish:
function generateValue(value) {
let max = 10,
min = 0,
range = 2,
newValue = value; // Start at baseline value
newValue += (2 * Math.random() - 1) * range; // Generate new value within +/- range
return (newValue < min ? min : // Cannot be lower than min
newValue > max ? max : // Cannot be greater than max
newValue);
}

Javascript random number between two digits [duplicate]

Is there a way to generate a random number in a specified range with JavaScript ?
For example: a specified range from 1 to 6 were the random number could be either 1, 2, 3, 4, 5, or 6.
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
const rndInt = randomIntFromInterval(1, 6)
console.log(rndInt)
What it does "extra" is it allows random intervals that do not start with 1.
So you can get a random number from 10 to 15 for example. Flexibility.
Important
The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.
If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:
const rndInt = Math.floor(Math.random() * 6) + 1
console.log(rndInt)
Where:
1 is the start number
6 is the number of possible results (1 + start (6) - end (1))
Math.random()
Returns an integer random number between min (included) and max (included):
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Or any random number between min (included) and max (not included):
function randomNumber(min, max) {
return Math.random() * (max - min) + min;
}
Useful examples (integers):
// 0 -> 10
Math.floor(Math.random() * 11);
// 1 -> 10
Math.floor(Math.random() * 10) + 1;
// 5 -> 20
Math.floor(Math.random() * 16) + 5;
// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;
** And always nice to be reminded (Mozilla):
Math.random() does not provide cryptographically secure random
numbers. Do not use them for anything related to security. Use the Web
Crypto API instead, and more precisely the
window.crypto.getRandomValues() method.
Other solutions:
(Math.random() * 6 | 0) + 1
~~(Math.random() * 6) + 1
Try online
TL;DR
function generateRandomInteger(min, max) {
return Math.floor(min + Math.random()*(max - min + 1))
}
To get the random number
generateRandomInteger(-20, 20);
EXPLANATION BELOW
integer - A number which is not a fraction; a whole number
We need to get a random number , say X between min and max.
X, min and max are all integers
i.e
min <= X <= max
If we subtract min from the equation, this is equivalent to
0 <= (X - min) <= (max - min)
Now, lets multiply this with a random number r
which is
0 <= (X - min) * r <= (max - min) * r
Now, lets add back min to the equation
min <= min + (X - min) * r <= min + (max - min) * r
For, any given X, the above equation satisfies only when r has range of [0,1] For any other values of r the above equation is unsatisfied.
Learn more about ranges [x,y] or (x,y) here
Our next step is to find a function which always results in a value which has a range of [0,1]
Now, the range of r i.e [0,1] is very similar to Math.random() function in Javascript. Isn't it?
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)
Random Function using Math.random() 0 <= r < 1
Notice that in Math.random() left bound is inclusive and the right bound is exclusive. This means min + (max - min) * r will evaluate to having a range from [min, max)
To include our right bound i.e [min,max] we increase the right bound by 1 and floor the result.
function generateRandomInteger(min, max) {
return Math.floor(min + Math.random()*(max - min + 1))
}
To get the random number
generateRandomInteger(-20, 20);
Or, in Underscore
_.random(min, max)
var x = 6; // can be any number
var rand = Math.floor(Math.random()*x) + 1;
jsfiddle: https://jsfiddle.net/cyGwf/477/
Random Integer: to get a random integer between min and max, use the following code
function getRandomInteger(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
Random Floating Point Number: to get a random floating point number between min and max, use the following code
function getRandomFloat(min, max) {
return Math.random() * (max - min) + min;
}
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Math is not my strong point, but I've been working on a project where I needed to generate a lot of random numbers between both positive and negative.
function randomBetween(min, max) {
if (min < 0) {
return min + Math.random() * (Math.abs(min)+max);
}else {
return min + Math.random() * max;
}
}
E.g
randomBetween(-10,15)//or..
randomBetween(10,20)//or...
randomBetween(-200,-100)
Of course, you can also add some validation to make sure you don't do this with anything other than numbers. Also make sure that min is always less than or equal to max.
Get a random integer between 0 and 400
let rand = Math.round(Math.random() * 400)
document.write(rand)
Get a random integer between 200 and 1500
let range = {min: 200, max: 1500}
let delta = range.max - range.min
const rand = Math.round(range.min + Math.random() * delta)
document.write(rand)
Using functions
function randBetween(min, max){
let delta = max - min
return Math.round(min + Math.random() * delta)
}
document.write(randBetween(10, 15));
// JavaScript ES6 arrow function
const randBetween = (min, max) => {
let delta = max - min
return Math.round(min + Math.random() * delta)
}
document.write(randBetween(10, 20))
I wrote more flexible function which can give you random number but not only integer.
function rand(min,max,interval)
{
if (typeof(interval)==='undefined') interval = 1;
var r = Math.floor(Math.random()*(max-min+interval)/interval);
return r*interval+min;
}
var a = rand(0,10); //can be 0, 1, 2 (...) 9, 10
var b = rand(4,6,0.1); //can be 4.0, 4.1, 4.2 (...) 5.9, 6.0
Fixed version.
ES6 / Arrow functions version based on Francis' code (i.e. the top answer):
const randomIntFromInterval = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
Example
Return a random number between 1 and 10:
Math.floor((Math.random() * 10) + 1);
The result could be:
3
Try yourself: here
--
or using lodash / undescore:
_.random(min, max)
Docs:
- lodash
- undescore
The top rated solution is not mathematically correct as same as comments under it -> Math.floor(Math.random() * 6) + 1.
Task: generate random number between 1 and 6.
Math.random() returns floating point number between 0 and 1 (like 0.344717274374 or 0.99341293123 for example), which we will use as a percentage, so Math.floor(Math.random() * 6) + 1 returns some percentage of 6 (max: 5, min: 0) and adds 1. The author got lucky that lower bound was 1., because percentage floor will "maximumly" return 5 which is less than 6 by 1, and that 1 will be added by lower bound 1.
The problems occurs when lower bound is greater than 1. For instance,
Task: generate random between 2 and 6.
(following author's logic)
Math.floor(Math.random() * 6) + 2, it is obviously seen that if we get 5 here -> Math.random() * 6 and then add 2, the outcome will be 7 which goes beyond the desired boundary of 6.
Another example,
Task: generate random between 10 and 12.
(following author's logic)
Math.floor(Math.random() * 12) + 10, (sorry for repeating) it is obvious that we are getting 0%-99% percent of number "12", which will go way beyond desired boundary of 12.
So, the correct logic is to take the difference between lower bound and upper bound add 1, and only then floor it (to substract 1, because Math.random() returns 0 - 0.99, so no way to get full upper bound, thats why we adding 1 to upper bound to get maximumly 99% of (upper bound + 1) and then we floor it to get rid of excess). Once we got the floored percentage of (difference + 1), we can add lower boundary to get the desired randomed number between 2 numbers.
The logic formula for that will be: Math.floor(Math.random() * ((up_boundary - low_boundary) + 1)) + 10.
P.s.: Even comments under the top-rated answer were incorrect, since people forgot to add 1 to the difference, meaning that they will never get the up boundary (yes it might be a case if they dont want to get it at all, but the requirenment was to include the upper boundary).
I was searching random number generator written in TypeScript and I have written this after reading all of the answers, hope It would work for TypeScript coders.
Rand(min: number, max: number): number {
return (Math.random() * (max - min + 1) | 0) + min;
}
Inspite of many answers and almost same result. I would like to add my answer and explain its working. Because it is important to understand its working rather than copy pasting one line code. Generating random numbers is nothing but simple maths.
CODE:
function getR(lower, upper) {
var percent = (Math.random() * 100);
// this will return number between 0-99 because Math.random returns decimal number from 0-0.9929292 something like that
//now you have a percentage, use it find out the number between your INTERVAL :upper-lower
var num = ((percent * (upper - lower) / 100));
//num will now have a number that falls in your INTERVAL simple maths
num += lower;
//add lower to make it fall in your INTERVAL
//but num is still in decimal
//use Math.floor>downward to its nearest integer you won't get upper value ever
//use Math.ceil>upward to its nearest integer upper value is possible
//Math.round>to its nearest integer 2.4>2 2.5>3 both lower and upper value possible
console.log(Math.floor(num), Math.ceil(num), Math.round(num));
}
Math.random() is fast and suitable for many purposes, but it's not appropriate if you need cryptographically-secure values (it's not secure), or if you need integers from a completely uniform unbiased distribution (the multiplication approach used in others answers produces certain values slightly more often than others).
In such cases, we can use crypto.getRandomValues() to generate secure integers, and reject any generated values that we can't map uniformly into the target range. This will be slower, but it shouldn't be significant unless you're generating extremely large numbers of values.
To clarify the biased distribution concern, consider the case where we want to generate a value between 1 and 5, but we have a random number generator that produces values between 1 and 16 (a 4-bit value). We want to have the same number of generated values mapping to each output value, but 16 does not evenly divide by 5: it leaves a remainder of 1. So we need to reject 1 of the possible generated values, and only continue when we get one of the 15 lesser values that can be uniformly mapped into our target range. Our behaviour could look like this pseudocode:
Generate a 4-bit integer in the range 1-16.
If we generated 1, 6, or 11 then output 1.
If we generated 2, 7, or 12 then output 2.
If we generated 3, 8, or 13 then output 3.
If we generated 4, 9, or 14 then output 4.
If we generated 5, 10, or 15 then output 5.
If we generated 16 then reject it and try again.
The following code uses similar logic, but generates a 32-bit integer instead, because that's the largest common integer size that can be represented by JavaScript's standard number type. (This could be modified to use BigInts if you need a larger range.) Regardless of the chosen range, the fraction of generated values that are rejected will always be less than 0.5, so the expected number of rejections will always be less than 1.0 and usually close to 0.0; you don't need to worry about it looping forever.
const randomInteger = (min, max) => {
const range = max - min;
const maxGeneratedValue = 0xFFFFFFFF;
const possibleResultValues = range + 1;
const possibleGeneratedValues = maxGeneratedValue + 1;
const remainder = possibleGeneratedValues % possibleResultValues;
const maxUnbiased = maxGeneratedValue - remainder;
if (!Number.isInteger(min) || !Number.isInteger(max) ||
max > Number.MAX_SAFE_INTEGER || min < Number.MIN_SAFE_INTEGER) {
throw new Error('Arguments must be safe integers.');
} else if (range > maxGeneratedValue) {
throw new Error(`Range of ${range} (from ${min} to ${max}) > ${maxGeneratedValue}.`);
} else if (max < min) {
throw new Error(`max (${max}) must be >= min (${min}).`);
} else if (min === max) {
return min;
}
let generated;
do {
generated = crypto.getRandomValues(new Uint32Array(1))[0];
} while (generated > maxUnbiased);
return min + (generated % possibleResultValues);
};
console.log(randomInteger(-8, 8)); // -2
console.log(randomInteger(0, 0)); // 0
console.log(randomInteger(0, 0xFFFFFFFF)); // 944450079
console.log(randomInteger(-1, 0xFFFFFFFF));
// Error: Range of 4294967296 covering -1 to 4294967295 is > 4294967295.
console.log(new Array(12).fill().map(n => randomInteger(8, 12)));
// [11, 8, 8, 11, 10, 8, 8, 12, 12, 12, 9, 9]
to return 1-6 like a dice basically,
return Math.round(Math.random() * 5 + 1);
Adding float with fixed precision version based on the int version in #Francisc's answer:
function randomFloatFromInterval (min, max, fractionDigits) {
const fractionMultiplier = Math.pow(10, fractionDigits)
return Math.round(
(Math.random() * (max - min) + min) * fractionMultiplier,
) / fractionMultiplier
}
so:
randomFloatFromInterval(1,3,4) // => 2.2679, 1.509, 1.8863, 2.9741, ...
and for int answer
randomFloatFromInterval(1,3,0) // => 1, 2, 3
Crypto-strong random integer number in range [a,b] (assumption: a < b )
let rand= (a,b)=> a+(b-a+1)*crypto.getRandomValues(new Uint32Array(1))[0]/2**32|0
console.log( rand(1,6) );
This function can generate a random integer number between (and including) min and max numbers:
function randomNumber(min, max) {
if (min > max) {
let temp = max;
max = min;
min = temp;
}
if (min <= 0) {
return Math.floor(Math.random() * (max + Math.abs(min) + 1)) + min;
} else {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
Example:
randomNumber(-2, 3); // can be -2, -1, 0, 1, 2 and 3
randomNumber(-5, -2); // can be -5, -4, -3 and -2
randomNumber(0, 4); // can be 0, 1, 2, 3 and 4
randomNumber(4, 0); // can be 0, 1, 2, 3 and 4
Using random function, which can be reused.
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
randomNum(1, 6);
This should work:
const getRandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
If the starting number is 1, as in your example (1-6), you can use Math.ceil() method instead of Math.floor().
Math.ceil(Math.random() * 6)
instead of
Math.floor(Math.random() * 6) + 1
Let's not forget other useful Math methods.
This is about nine years late, but randojs.com makes this a simple one-liner:
rando(1, 6)
You just need to add this to the head of your html document, and you can do pretty much whatever you want with randomness easily. Random values from arrays, random jquery elements, random properties from objects, and even preventing repetitions if needed.
<script src="https://randojs.com/1.0.0.js"></script>
Try using:
function random(min, max) {
return Math.round((Math.random() *( Math.abs(max - min))) + min);
}
console.log(random(1, 6));
Short Answer: It's achievable using a simple array.
you can alternate within array elements.
This solution works even if your values are not consecutive. Values don't even have to be a number.
let array = [1, 2, 3, 4, 5, 6];
const randomValue = array[Math.floor(Math.random() * array.length)];
This simple function is handy and works in ANY cases (fully tested).
Also, the distribution of the results has been fully tested and is 100% correct.
function randomInteger(pMin = 1, pMax = 1_000_000_000)
//Author: Axel Gauffre.
//Here: https://stackoverflow.com/a/74636954/5171000
//Inspired by: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values
//
//This function RETURNS A RANDOM INTEGER between pMin (INCLUDED) and pMax (INCLUDED).
// - pMin and pMax should be integers.
// - HOWEVER, if pMin and/or pMax are FLOATS, they will be ROUNDED to the NEAREST integer.
// - NEGATIVE values ARE supported.
// - The ORDER of the 2 arguments has NO consequence: If pMin > pMax, then pMin and pMax will simply be SWAPPED.
// - If pMin is omitted, it will DEFAULT TO 1.
// - If pMax is omitted, it will DEFAULT TO 1 BILLION.
//
//This function works in ANY cases (fully tested).
//Also, the distribution of the results has been fully tested and is 100% correct.
{
pMin = Math.round(pMin);
pMax = Math.round(pMax);
if (pMax < pMin) { let t = pMin; pMin = pMax; pMax = t;}
return Math.floor(Math.random() * (pMax+1 - pMin) + pMin);
}
I discovered a great new way to do this using ES6 default parameters. It is very nifty since it allows either one argument or two arguments. Here it is:
function random(n, b = 0) {
return Math.random() * (b-n) + n;
}
This works for me and produces values like Python's random.randint standard library function:
function randint(min, max) {
return Math.round((Math.random() * Math.abs(max - min)) + min);
}
console.log("Random integer: " + randint(-5, 5));

Creating a generic data overflow function using bit shifting or math in javascript

Use Case
I am learning functional programming. During this exercise, I wanted to create a function that naturally overflows. As an example, if the range of the data is from 2-12, if the value of the input is 12 and you add 1 to it, it becomes 2. Likewise, if you subtract 1 from 2, it becomes 12. It should be noted that I want to improve my math skills because I haven't been thinking like a mathematician for the past 15 years. So forgive me if this looks fairly trivial.
Implementation
The first implementation was the following:
var confineRange(value, min, max) {
var newValue = value;
while (newValue > max) {
newValue -= max;
}
while (newValue < min) {
newValue = max - (min - newValue);
}
return newValue;
}
Fiddle Here
This implementation worked, but seeing a loop, meant that it could be improved. So the second version I attempted was this:
function confineRange(value, min, max) {
if (value > 0)
return (value % max - min)
if (value < 0)
return ((value + max) % max) - min
if (value == 0)
return max - min;
}
However, this version looks like it could be simplified even more.
Questions
How could this be encapsulate in a single mathematical function?
Would it be possible to create this function using bit-shifting? If so, how?
Is there a more elegant solution using native javascript functions?
First for simplicity let's see for positive integers only, assuming that the max > min relation of the parameters are met, and value is larger or equal to min:
function confineRange(value, min, max) {
return ((value - min) % (max - min + 1)) + min;
}
The bottom end of your range is min, and you want to confine within a max - min + 1 range. For example with your example of min = 2 and max = 12 the result can get 11 distinct values! (And not 10 like it may seem first).
Let's test it with the edge cases:
confineRange(2, 2, 12);
This will execute ((2 - 2) % (12 - 2 + 1)) + 2, which results 2, good.
confineRange(12, 2, 12);
This will execute ((12 - 2) % (12 - 2 + 1)) + 2, which results 12, also good. Note that 12 - 2 makes 10, which is the largest integer which will pass through % 11 unmodified. Now let's try with 13:
confineRange(13, 2, 12);
Now after partially calculating it, it will look like (11 % 11) + 2, which will make 2 as expected (that is if that 13 was reached as 12 + 1, then it will seem to properly wrap around to 2).
So the basic idea is this.
The function however might fail with numbers below 2 due to how Javascript may or may not handle negative numbers, for example see this article. I don't know whether the behaviour is standardized or particular browsers may or may not try to fix it, so better assume this undefined.
The problem now is that you have no possibility of feeding negative numbers in the '%' operator, but given this interface you can only solve the problem with '%' (modulo). So an other method has to be employed.
JavaScript has a Math.floor() function which always rounds downwards (check it here). Using this can eliminate the problem of negatives, so let's construct our own modulo from it:
function confineRange(value, min, max) {
t = value - min;
d = max - min + 1;
t = t - (Math.floor(t / d) * d);
return t + min;
}
The modulo happens on the third (t = t - (Math.floor(t / d) * d);) line, this calculates t % d independent of sign. This solution will work properly for numbers below 2 as well, so you will for example get 12 if you supply 1 as value with your example.
For your second question the problem is solvable with bit manipulation, but only if the range (the number of distinct values you want to get) is a power of 2. This case applying a proper AND (&) mask on the value will do the job.

Show different image on each page load

I have this code which simply changes 4 images on each page load.
<HEAD>
<script language="Javascript">
function RandomizeImage()
{
var imagesarray = new Array("001.png", "002.png", "003.png", "004.png")
var randomnumber = Math.round(Math.random()*(imagesarray.length - 1))
document.images.someimage.src = imagesarray[randomnumber]
}
</script>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js"></script>
</HEAD>
<BODY onload="RandomizeImage()" bgcolor="#000000">
<div id="bg">
<IMG name="someimage" src="001.png" border="0" onclick="location.href='mailto:me#xxx.com?Subject=Hello'" id="bgg">
</div>
</BODY>
However, on most page reloads I get image "001.png" or "002.png". Can anyone tell me why?
You can use this function:
function getRandom(min,max)
{
var res = Math.floor(Math.random() * (max+1 - min) + min);
return res;
}
Example: If you want to return a value from (1,2,3,4,5) you should chose min 1 and max 5.
var rImage = '00'+getRandom(1,5)+'.png';
console.log(rImage);
// output: 001.png or 002.png or 003.png or 004.png or 005.png
Edited
Hope to help
Use Math.floor() instead of .round() and don't subtract 1 from the length:
var randomnumber = Math.floor(Math.random() * imagesarray.length);
Edit: Math.random() * 3 will give us numbers between 0 (inclusive) and 3 (exclusive). Using Math.round() the values that result in each number:
0: 0.0 — 0.4999
1: 0.5 — 1.4999
2: 1.5 — 2.4999
3: 2.5 — 2.9999
You can see that the high and low numbers each of half the probability of being selected as the numbers in the middle. This is because you will never get numbers in the range -0.5 — -0.9999 or 3.0 — 3.4999 giving these end numbers half the likelihood of being selected.
Math.floor() eliminates this problem because all the numbers fall within the range. Math.random() * 4 will give us numbers between 0 (inclusive) and 4 (exclusive). Using Math.floor() the values that result in each number:
0: 0.0 — 0.9999
1: 1.0 — 1.9999
2: 2.0 — 2.9999
3: 3.0 — 3.9999
You may find it useful to use a utility function such as this one:
function randomInt(max, min) {
min = min || 0;
var Max = Math.max(max, min) + 1;
var Min = Math.min(max, min);
return Math.floor(Math.random() * (Max - Min)) + Min;
}
The nice thing about this function is that the order of the arguments doesn't matter, and min is optional. Note that, since max is inclusive in this function, you would want to subtract 1 from the array length:
var randomnumber = randomInt(imagesarray.length - 1);
Math.round has this effect:
0 <= x < 0.5 => 0
0.5 <= x < 1.5 => 1
1.5 <= x < 2.5 => 2
..
(n-0.5) <= x < n => n
as you can see the first and last number has less probability to be fetched.
So a better approach is to use Math.floor as specified here

Categories

Resources