I'm trying to generate random numbers in javascript that are evenly distributed between 2 floats. I've tried using the method from the mozilla docs to get a random number between 2 values but it appears to cluster on the upper end of the distribution. This script:
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function median(values) {
if (values.length === 0) throw new Error("No inputs");
values.sort(function (a, b) {
return a - b;
});
var half = Math.floor(values.length / 2);
if (values.length % 2)
return values[half];
return (values[half - 1] + values[half]) / 2.0;
}
const total = 10_000_000
let acc = []
for (i = 0; i < total; i++) {
acc.push(getRandomArbitrary(1e-10, 1e-1))
}
console.log(median(acc))
consistently outputs a number close to .05 instead of a number in the middle of the range (5e-5). Is there any way to have the number be distributed evenly?
Thank you!
EDIT: changed script to output median instead of mean.
function log10(x) { return Math.log(x)/Math.LN10; }
function getLogRandomArbitrary(min, max) {
return Math.pow(10, log10(min) + (Math.random() * (log10(max) - log10(min))));
}
function median(values) {
if(values.length === 0) throw new Error("No inputs");
let a = [...values].sort((a,b)=>a-b);
return a[Math.floor(a.length/2)];
}
const iterations = 1_000_000;
let a = [];
for (let i=0; i<iterations; i++) {
a.push(getLogRandomArbitrary(1e-10, 1e-1));
}
console.log(median(a));
console.log(log10(median(a)));
I am trying to make a Random Number Generator.
I made a code and it does work well.
document.querySelector('#btn').addEventListener('click',()=>{
generate(1,45,6)
});
function generate(min, max, count){
const arr = [];
if(min >= max) return;
if(max - min + 1 < count) return;
while (arr.length < count) {
let num = Math.floor(Math.random() * max) + min;
let flag = arr.every((i) => {
return i === num ? false : true;
});
if (flag) {
arr.push(num);
}
}
console.log(arr);
}
<button id="btn">Gen</button>
But my algorithm's time complexity is O(n). (I am not sure, I didn't calculate it strictly)
I hope to reduce the time complexity if I can.
And, I guess my above code can be compacted, but I can't.
Summary What I Want
To reduce the time complexity if it can be
To make it compacted
You can use a Set (which will take care of duplicates) instead of an array and keep checking its size until you have all the numbers you want:
document.querySelector('#btn').addEventListener('click',()=>{
generate(1,45,6)
});
function generate(min, max, count){
const s = new Set();
if(min >= max) return;
if(max - min + 1 < count) return;
while (s.size < count) { // O(1)
let num = Math.floor(Math.random() * max) + min;
s.add(num); // O(1)
}
console.log(Array.from(s));
}
<button id="btn">Gen</button>
I want get array with random unique figures.
I make cycle for
var row = [];
var count = 10;
function getRandomArbitary(min, max) {
return Math.random() * (max - min) + min;
}
function searchRandom() {
var rdm = Math.floor(getRandomArbitary(1, 20));
for (var i = 0; i < row.length; i++) {
if (rdm == row[i]) {
searchRandom();
}
}
row.push(rdm);
}
And then if I want 10 figures in array I make next cycle
for (var i = 0; i < count; i++) {
searchRandom();
}
console.log(row);
But it doesn't work (
Wouldn't it be easier to just make the function take the options as arguments and return the array ?
function randomArray(count, min, max) {
if (count > (max - min)) return;
var arr = [], t;
while (count) {
t = Math.floor(Math.random() * (max - min) + min);
if (arr.indexOf(t) === -1) {
arr.push(t);
count--;
}
}
return arr;
}
console.log(randomArray(10, 1, 20));
I think and i havent ran it on my system
but this should work
function searchRandom() {
var rdm = Math.floor(getRandomArbitary(1, 20));
for (var i = 0; i < row.length; i++) {
if (rdm == row[i]) {
searchRandom();
return;
}
}
row.push(rdm);
}
Notice the return statement after the nested searchRandom()
This will stop you from getting into incorrect cases for example lets say till now the row has [1,2]. and the next random no is 1;
in that case why you are recursively calling the search method??
you can just loop from 1 to len (or 0 to len-1), and wait until you generated the random number that is not present simply by a while loop. here is an example
var generateRandomArray = function(len, min, max) {
if(len> (max-min)) {return;}
var array = [],
getRandomArbitary = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
},
idx;
for (idx = 0; idx < len; idx++) {
var num;
while (array.includes(num = getRandomArbitary(min,max))) {}
array.push(num)
}
return array;
}
console.log('Random Array of 5 element from 10 to 20: ', generateRandomArray(5, 10,20))
I am trying to write a function which produces four unequal random numbers in a given range, but the function is currently failing at the while (selection[i] in selection.slice().splice(i) line. This line should check whether the current (i'th) value is shared by any of the other random values but at the moment it seems to do nothing - perhaps I have used in incorrectly? Any help would be appreciated.
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
selected=[];
function randomSelection() {
var notselected=[];
for (var i=0; i<25; i++) {
if(!contains(selected, i)) {
notselected.push(i);
}
}
var selection=[notselected[Math.floor(Math.random() * notselected.length)],
notselected[Math.floor(Math.random() * notselected.length)],
notselected[Math.floor(Math.random() * notselected.length)],
notselected[Math.floor(Math.random() * notselected.length)]];
for (var i=0; i<selection.length; i++) {
while (selection[i] in selection.slice().splice(i)) {
alert('Hello!')
selection[i] = notselected[Math.floor(Math.random() * notselected.length)];
}
}
for (var i=0; i<selection.length; i++) {
selected.pop(selection[i]);
}
}
You can obtain a random value between two numbers using the following method
function getRandomArbitrary(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
If the value needs to be an integer you can use the following method:
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
So, supposing that you need 4 different random integer values you could do something like that
var randoms = [];
while(randoms.length < 4) {
var random = getRandomInt(0, 25);
if(randoms.indexOf(random) === -1) {
randoms.push(random);
}
}
To randomly shuffle a set of objects (numbers in this case)
var values = [0,1,2,3,4,5,6];
function shuffle(arr){
var temp = [...arr];
arr.length = 0;
while(temp.length > 0){
arr.push(temp.splice(Math.floor(Math.random() * temp.length),1)[0]);
}
return arr;
}
console.log("pre shuffle : [" + values.join(", ") + "]");
shuffle(values);
console.log("post shuffle : [" + values.join(", ") + "]");
I'm generating random numbers from 1 to 20 by calling generateRandom(). How can I exclude some values, say 8 and 15?
function generateRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var test = generateRandom(1, 20)
it should be or instead of and
function generateRandom(min, max) {
var num = Math.floor(Math.random() * (max - min + 1)) + min;
return (num === 8 || num === 15) ? generateRandom(min, max) : num;
}
var test = generateRandom(1, 20)
One way, which will maintain the generator's statistical properties, is to generate a number in [1, 18]. Then apply, in this order:
If the number is 8 or more, add 1.
If the number is 15 or more, add 1.
I'd be reluctant to reject and re-sample as that can cause correlation plains to appear in linear congruential generators.
Right now I'm using this and it works without causing browser issues with infinities loops, also tested in mobile devices (using Ionic/Cordova):
function getRandomIndex(usedIndexs, maxIndex) {
var result = 0;
var min = 0;
var max = maxIndex - 1;
var index = Math.floor(Math.random()*(max-min+1)+min);
while(usedIndexs.indexOf(index) > -1) {
if (index < max) {
index++;
} else {
index = 0;
}
}
return index;
}
To generate random number between 1 and 20 excluding some given numbers, you can simply do this:
function generateRandom(min, max, exclude) {
let random;
while (!random) {
const x = Math.floor(Math.random() * (max - min + 1)) + min;
if (exclude.indexOf(x) === -1) random = x;
}
return random;
}
const test = generateRandom(1, 20, [8, 15]);
/**
* Returns a random integer between min (inclusive) and max (inclusive).
* Pass all values as an array, as 3rd argument which values shouldn't be generated by the function.
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
const minimum = Math.ceil(min);
const maximum = Math.floor(max);
return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
}
function getRandomIntExcludingExistingNumbers(min, max, excludeArrayNumbers) {
let randomNumber;
if(!Array.isArray(excludeArrayNumbers)) {
randomNumber = getRandomInt(min, max);
return randomNumber;
}
do {
randomNumber = getRandomInt(min, max);
} while ((excludeArrayNumbers || []).includes(randomNumber));
return randomNumber;
}
const randomNumber = getRandomIntExcludingExistingNumbers(1, 10, [1, 2, 4, 5, 9]);
// It will return random integer between 1 to 10 excluding 1,2,4,5,9
Explanation:
getRandomInt function generates random numbers between min and max values.
I am utilizing that function to make "getRandomIntExcludingExistingNumbers" function to avoid specific values.
we will simply call getRandomInt(min, max) values.
Then in do while loop we will check if randomly generated values belongs to any of the values which shouldn't be generated.
If it is unique integer outside exclude values then we will return the value.
If our value is from the excluded values, then from do -- while loop, we will once again call getRandomInt to generate new values.
Here is a slightly modified answer that is similar to all the others but it allows your to pass a single or an array of failing numbers
function generateRandom(min, max, failOn) {
failOn = Array.isArray(failOn) ? failOn : [failOn]
var num = Math.floor(Math.random() * (max - min + 1)) + min;
return failOn.includes(num) ? generateRandom(min, max, failOn) : num;
}
You could make use of a recursive function
function generateRandom(min, max, num1, num2) {
var rtn = Math.floor(Math.random() * (max - min + 1)) + min;
return rtn == num1 || rtn == num2 ? generateRandom(min, max, num1, num2) : rtn;
}
I think it should be like this, if you want good distribution on all numbers.
and, for this solution, it is required to higher max than 15 and lower min that 8
function generateRandom(min, max) {
var v = Math.floor(Math.random() * (max - min + 1 - 2)) + min;
if (v == 8) return max-1;
else if (v == 15) return max-2;
else return v;
}
var test = generateRandom(1, 20)
You can build an array dynamically. Depending on where you are getting the excluded numbers. Something like:
var excluded = [8, 15];
var random = [];
for(var i = min; i <= max; i++) {
if(excluded.indexOf(i) !== -1) {
random.push(i);
}
}
Then use the tips found in the answer for this post: How can I generate a random number within a range but exclude some?. Should get you to where you want to go.
Here is a really stupidly overcomplicated solution...
<script>
var excludedNumbers = [];
excludedNumbers.push(8);
excludedNumbers.push(15);
excludedNumbers.push(10);
var array = generateExclude(excludedNumbers, 1, 20);
function generateExclude(excludedNumbers, min, max){
var newNumbers = [];
for(var i = min; i <= max; i++) {
for(var j = 0; j < excludedNumbers.length; j++) {
var checker = $.inArray(i, excludedNumbers)
if(checker > -1){
}else{
if($.inArray(i, newNumbers)<= -1){
newNumbers.push(i);
}
}
};
};
return newNumbers;
}
function generateRandomNumbers(items){
var num = items[Math.floor(Math.random()*items.length)];;
return num;
}
console.log(generateRandomNumbers(array))
</script>
I have answered a similar question for Java: Generate random numbers except certain values. I just copy and paste the answer as follows.
Actually, we do not need to use contains(random) with a while loop.
To simplify the question, let's see what happens if we only have one excluding value. We can split the result to 2 parts. Then the number of possible values is range-1. If the random number is less than the excluded value, just return it. Otherwise, we could add 1.
For multiple excluding values, We can split the result set into size+1 parts, where size means the number of excluding values. Then the number of possible values is range-size. Then we sort excluding values in ascending order. If random number is less than the excluding value minus i, then we just return the random number add i, where i is the index of the the excluding value.
public int generateRandomNumberWithExcepts(int start, int end, List<Integer> excepts) {
int size = excepts.size();
int range = end - start + 1 - size;
int randNum = random.nextInt(range) + start;
excepts.sort(null); // sort excluding values in ascending order
int i=0;
for(int except : excepts) {
if(randNum < except-i){
return randNum + i;
}
i++;
}
return randNum + i;
}
I've read through all these answers and they differ a lot in philosophy, so I thought I might add my very own 2 bits, despite of this question having an answer, because I do think there is a better and more elegant way of approaching this problem.
We can make a function that takes min, max and blacklist as parameters and outputs a random result without using recursion (and with close to 0 if statements):
const blrand = function(min, max, blacklist) {
if(!blacklist)
blacklist = []
let rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
let retv = 0;
while(blacklist.indexOf(retv = rand(min,max)) > -1) { }
return retv;
}
usage:
let randomNumber = blrand(0, 20, [8, 15]);
You can simply do like this
function generatedRandExclude(showed,max) {
let randNo = -1;
while(showed.length < max) {
randNo = Math.floor(Math.random() * Math.floor(max));
if(!showed.includes(randNo)) {
showed.push(randNo);
break;
}
}
return randNo;
}
let showed = [];
function run() {
console.log(generatedRandExclude(showed,6));
}
run();
run();
run();
run();
generatedRandExclude generate random number excluded using array showed.
This is a simple and neat idea, I am a electromechanical engineer and I am just learning JS.
This is going to print a random numeber between 1 and 100.
Except 8 and 15
var r; // this is the random integer.
var val; //this will serve as validator for the random integer.
val=0;
while(val==0) {
r=Math.round(Math.random()*100)+1;
if(r!=8 && r!=15){val=1;} //a valid number will be any number different from 8 and 15
//then validator will change and go out from the loop.
}
document.write(r);
You could take an offset for random values greater or equal than zerow ith a sorted (ascending) array and return a sum with adjusted random value.
const
getRandomWithoutZero = (lower, upper, gaps) => () => {
const r = Math.floor(Math.random() * (upper - lower + 1 - gaps.length) + lower);
return gaps.reduce((s, g) => s + (s >= g), r);
},
random = getRandomWithoutZero(-9, 9, [-3, 0, 4]),
count = {};
for (let i = 0; i < 1.6e6; i++) {
const r = random();
count[r] = (count[r] || 0) + 1;
}
console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }