Generate Random elements without duplicate - javascript

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>

Related

Generate Uniform Distribution of Floats in Javascript

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)));

condition exponentially less likely per loop pass

I want to achieve a condition inside a for loop, which will get exponentially less likely to be true after every loop pass.
Here is a simplified version of my current, linear solution: every loop pass it is x+1 less likely that the random number === 0 and thus the probability per run is reduced.
for (let x = 0; x < 10; x++) {
if (getRandomInt(0,x) === 0) {
// do something
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
But I don't know how to change the condition so that the probability per loop pass becomes exponential and not linearly smaller as in my solution.
Anyone an idea? Thanks for your time!
Instead of increasing x by adding, increment it by multiplying.
for (let x = 1; x < 128; x *= 2) {
if (getRandomInt(0, x) == 0) {
// do something
}
}
If i understand what you are looking for couldn't you just do following?
for (let x = 0; x < 10; x++) {
if (getRandomInt(0,Math.pow(2, x)) === 0) {
// do something
}
}

Fill an array with distanced random integers

I need an array to be filled with random integers
Those integers should be very distinct from each other i.e. must at least be 20 units of separation between each items
This is what i have tried so far :
var all = [];
var i = 0;
randomDiff();
function randomDiff() {
var num1 = randomNumber(10, 290); //chose a first random num in the range...
all[0] = num1; //...put it in first index of array
do // until you have 12 items...
{
var temp = randomNumber(10, 290); //...you pick a temporary num
var j;
for (j = 0; j < all.length; j++) // for each item already in the array
{
if ((temp < all[i] - 10) || (temp > all[i] + 10)) // if the temporary num is different enough from others members...
{
all.push(temp); //then you can store it
i++; //increment until....
console.log(all[i]);
}
}
}
while (i < 11) // ...it is filled with 12 items in array
}
////////////Radom in int range function///////////////////////////////////////
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
but always unsuccessful, including infinite loops...
Have a look on something like this:
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
const LIST_SIZE = 20;
const DISTANCE = 10;
const STOP_AFTER_ATTEMPT = 2000;
const randomList = [];
let attempt = 0;
while(randomList.length < LIST_SIZE && attempt < STOP_AFTER_ATTEMPT) {
const num = randomNumber(10, 290);
const numberExistsWithSmallerDistance = randomList.some(r => Math.abs(r - num) < DISTANCE)
if (!numberExistsWithSmallerDistance) {
randomList.push(num);
}
attempt++;
}
if (randomList.length === LIST_SIZE) {
console.log(randomList);
} else {
console.log("Failed to create array with distnct values after ", attempt, " tries");
}
Here's a solution that will always work, as long as you allow enough room in the range/separation/count you choose. And it's way more efficient than a while loop. It doesn't just keep trying until it gets it right, it actually does the math to make sure it's right the first time.
This comes at the cost of tending to lean towards certain numbers more than others (like from + (i * separation)), so take note of that.
function getSeparatedRadomInts(from, through, separation, count) {
if(through < from) return getSeparatedRadomInts(through, from, separation, count);
if(count == 0) return [];
if(separation == 0) return !!console.log("Please allow enough room in the range/separation/count you choose.");
//pick values from pool of numbers evenly stepped apart by units of separation... adding 1 to from and through if from is 0 so we can divide properly
var smallFrom = Math.ceil((from || 1) / separation);
var smallThrough = Math.floor((through + (from == 0)) / separation);
var picks = randoSequence(smallFrom, smallThrough).slice(-count).sort((a, b) => a - b);
if(picks.length < count) return !!console.log("Please allow enough room in the range/separation/count you choose.");
for (var i = 0; i < picks.length; i++) picks[i] *= separation;
//go through each pick and randomize with any wiggle room between the numbers above/below it... adding 1 to from and through if from is 0
for (var i = 0; i < picks.length; i++) {
var lowerBound = picks[i - 1] + separation || from || 1;
var upperBound = picks[i + 1] - separation || (through + (from == 0));
picks[i] = rando(lowerBound, upperBound);
}
//subtract 1 from all picks in cases where from is 0 to compensate for adding 1 earlier
for (var i = 0; i < picks.length; i++) if(from == 0) picks[i] = picks[i] - 1;
return picks;
}
console.log(getSeparatedRadomInts(10, 290, 20, 12));
<script src="https://randojs.com/1.0.0.js"></script>
To be clear, from is the minimum range value, through is the maximum range value, separation is the minimum each number must be apart from each other (a separation of 20 could result in [10, 30, 50, 70], for example), and count is how many values you want to pick.
I used randojs in this code to simplify the randomness and make it easier to read, so if you want to use this code, just remember to paste this in the head of your HTML document:
<script src="https://randojs.com/1.0.0.js"></script>

Random non repeating array JS (solving without .includes())

I need array with random non repeating values. I find solving with includes() but i want make without it.
CODE
function rand(min, max){
return Math.round( Math.random() * (max - min) + min);
}
function getRandArray(n, min, max) {
//n - array length
var randArr = [];
randArr[0] = rand(min, max);
for (var i = 0; i < n; i++) {
var randNum = rand(min, max);
for (var j = 0; j < randArr.length; j++){
if (randNum != randArr[j])
randArr[i] = randNum;
else
randNum = rand(min, max);
}
}
return randArr;
}
You can leverage the power of the ES6 Set to do this very easily. Since you are looking for an array output, you can simply use Array.from and pass in the set to return from the function. Here's what it would look like:
function rand(min, max){
return Math.round( Math.random() * (max - min) + min);
}
function getRandArr(n, min, max) {
var set = new Set();
// ensure that the number of unique numbers they want is possible
var maxNumsInArr = Math.min(n, max-min+1);
while(set.size < maxNumsInArr) {
set.add(rand(min, max));
}
return Array.from(set);
}
console.log(getRandArr(10, 0, 10));
console.log(getRandArr(5, 100, 399));
console.log(getRandArr(5, 0, 2)); // only 3 possible unique values, so length will be 3
If ES6 is not an option you can convert the random numbers into String keys for an object and take advantage of the fact that an object will not allow duplicate properties like this:
function rand(min, max){
return Math.round( Math.random() * (max - min) + min);
}
function getRandArr(n, min, max) {
if (n > (max - min + 1)) {
throw "Cannot create array of size " + n;
}
var res = {};
while (Object.keys(res).length < n) {
var r = rand(min, max);
res[r] = true;
}
var array = Object.keys(res);
return array;
}
console.log(getRandArr(100, 0, 10000));
You could always convert the array of Strings back to numbers with a single pass after you have the results.
Adding a property to an object will use a hash of the property name so you get O(1) time for checking if the number is unique.

JavaScript generate random number except some values

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; }

Categories

Resources