Resetting Data in an Array If Any Elements Are Equal - javascript

I've been having an issue with some code in a project I'm doing at school. I have an array of length 4, where each element of the array is an integer. The values of the integers are determined through multiplying Math.random() by the length of another array. The goal of my code is to find some way of checking whether any of the elements are equal to each other, and if they are then I would want to "reset" (redefine) them until they aren't equal. Here's what I just described, albeit in a very basic form:
var originalArray=[a, b, c, d, e, f, g];
var or1=Math.floor(Math.random() * originalArray.length);
var or2=Math.floor(Math.random() * originalArray.length);
var or3=Math.floor(Math.random() * originalArray.length);
var or4=Math.floor(Math.random() * originalArray.length);
var numArray=[or1, or2, or3, or4];
function resetData(){
or1=Math.floor(Math.random() * originalArray.length);
or2=Math.floor(Math.random() * originalArray.length);
or3=Math.floor(Math.random() * originalArray.length);
or4=Math.floor(Math.random() * originalArray.length);
numArray=[or1, or2, or3, or4];
}
I then have another function, at the beginning of which I want to run resetData. However, if any of the elements are equal to each other then I want it to run again until none of them are.
function startOperations(){
resetData();
//insert other code here
}
I'm stuck on what I should do to make it only reset under the previously-mentioned conditions. My initial idea was to do a for-loop, kind of like this:
for(i=0; i<songNumArray.length-1; i++){
if(songNumArray[i]==songNumArray[i+1]){
resetData();
}
}
But I now realize that this won't work, since this only checks if the elements next to each other are equal. So I tried just an if-statement:
if(songNumArray[0]==songNumArray[1]||songNumArray[0]==songNumArray[2]||songNumArray[0]==songNumArray[3]){
resetData();
}
This doesn't work either, of course, which is why I'm here. Does anyone on here have an idea of what I could do?

Rather than declaring variables for each element of the array first and putting them into an array later, I'd suggest simply using a array to start with - that way you can use the array methods. You also don't need to reset all of the elements in the array and retry until something works - it would be more elegant to try to generate new elements and insert them only if they pass. For example:
function createArrayWithoutDuplicates(highestValMinusOne, length) {
const highestVal = highestValMinusOne + 1;
const arr = [];
while (arr.length < length) {
let val;
do {
val = Math.floor(Math.random() * highestVal);
} while (arr.includes(val));
arr.push(val);
}
return arr;
}
console.log(createArrayWithoutDuplicates(10, 7));
console.log(createArrayWithoutDuplicates(10, 7));
console.log(createArrayWithoutDuplicates(10, 7));
console.log(createArrayWithoutDuplicates(10, 7));
console.log(createArrayWithoutDuplicates(10, 7));
console.log(createArrayWithoutDuplicates(10, 7));

You can do it with 2 for loops where first one is from first element to second last element and the second loop always starts with the next element that of the first loop and ends with last element of the array.
for(i = 0; i < songNumArray.length - 1; i++){
for(j = i + 1; j < songNumArray.length; j++) {
if(songNumArray[i] == songNumArray[j]){
resetData();
}
}
}

You could use a Set like this to look for duplicates:
let dupes;
do {
let set = new Set();
dupes = false;
for (let i = 0; i < songNumArray.length; ++i) {
if (set.has(songNumArray[i]) {
resetData();
dupes = true;
break;
}
else {
set.add(songNumArray[i]);
}
}
} while (dupes);

I've added a getUniqueNumber function that generates a unique number if it's a duplicate.
Commented code below;
var originalArray = [1, 2, 3, 4, 5, 6, 7];
var or1 = Math.floor(Math.random() * originalArray.length);
var or2 = Math.floor(Math.random() * originalArray.length);
var or3 = Math.floor(Math.random() * originalArray.length);
var or4 = Math.floor(Math.random() * originalArray.length);
var numArray = [or1, or2, or3, or4];
function getUniqueNumber(index) {
var val = numArray[index];
// check if subsequent values are equal to current value.
// if not return current value.
// no need to check previous values as they are already unique.
if (numArray.indexOf(val, index + 1) === -1)
return val;
// repeat until unique number is generated
while (true) {
val = Math.floor(Math.random() * originalArray.length);
if (numArray.indexOf(val) === -1)
return val;
}
}
function resetData() {
for (let i = 0; i < 4; i++) {
numArray[i] = getUniqueNumber(i);
}
}
function startOperations() {
console.log("before: " + numArray);
resetData();
console.log("after: " + numArray);
//insert other code here
}
startOperations();

Just use another for loop to iterate again. Something like this:
for(i=0; i<songNumArray.length; i++){
for(j=i+1; j<songNumArray.length; j++){
if(songNumArray[i]==songNumArray[j]){
resetData();
}
}
}

I would use nested for-loops. That way, we can cross reference all elements of the array with each other.
function newData() {
for (var i = 0; i < originalArray.length; i++) {
for (var j = 0; j < originalArray.length; j++) {
if (i != j && numArray[i] == numArray[j]) {
console.log('find');
numArray[i] = Math.floor(Math.random() * originalArray.length);
i = 0;
}
}
}
}
If you look at the if-statement, you can see I first check to make sure it doesn't equal j, since a number will always equal itself, and we don't want to cross reference the same elements in the array. Next, I check if they're the same. If they are, I set i to 0. This acts similar to recursion, it "resets" or "re-runs" the loop. If a number gets set to an already existing number, it will go through the if statement again, modifying it until it meets the criteria. Hope this helps!

Related

How to prevent Math.random from repeating a return? [duplicate]

I have the following function
function randomNum(max, used){
newNum = Math.floor(Math.random() * max + 1);
if($.inArray(newNum, used) === -1){
console.log(newNum + " is not in array");
return newNum;
}else{
return randomNum(max,used);
}
}
Basically I am creating a random number between 1 - 10 and checking to see if that number has already been created, by adding it to an array and checking the new created number against it. I call it by adding it to a variable..
UPDATED:
for(var i=0;i < 10;i++){
randNum = randomNum(10, usedNums);
usedNums.push(randNum);
//do something with ranNum
}
This works, but in Chrome I get the following error:
Uncaught RangeError: Maximum call stack size exceeded
Which I guess it's because I am calling the function inside itself too many times. Which means my code is no good.
Can someone help me with the logic? what's a best way to make sure my numbers are not repeating?
If I understand right then you're just looking for a permutation (i.e. the numbers randomised with no repeats) of the numbers 1-10?
Maybe try generating a randomised list of those numbers, once, at the start, and then just working your way through those?
This will calculate a random permutation of the numbers in nums:
var nums = [1,2,3,4,5,6,7,8,9,10],
ranNums = [],
i = nums.length,
j = 0;
while (i--) {
j = Math.floor(Math.random() * (i+1));
ranNums.push(nums[j]);
nums.splice(j,1);
}
So, for example, if you were looking for random numbers between 1 - 20 that were also even, then you could use:
nums = [2,4,6,8,10,12,14,16,18,20];
Then just read through ranNums in order to recall the random numbers.
This runs no risk of it taking increasingly longer to find unused numbers, as you were finding in your approach.
EDIT: After reading this and running a test on jsperf, it seems like a much better way of doing this is a Fisher–Yates Shuffle:
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10]);
Basically, it's more efficient by avoiding the use of 'expensive' array operations.
BONUS EDIT: Another possibility is using generators (assuming you have support):
function* shuffle(array) {
var i = array.length;
while (i--) {
yield array.splice(Math.floor(Math.random() * (i+1)), 1)[0];
}
}
Then to use:
var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10]);
ranNums.next().value; // first random number from array
ranNums.next().value; // second random number from array
ranNums.next().value; // etc.
where ranNums.next().value will eventually evaluate to undefined once you've run through all the elements in the shuffled array.
Overall this won't be as efficient as the Fisher–Yates Shuffle because you're still splice-ing an array. But the difference is that you're now doing that work only when you need it rather than doing it all upfront, so depending upon your use case, this might be better.
//random number without repetition in JavaScript, Just in one line;
//it can be used as _id;
//it not need to store or check;
const myRnId = () => parseInt(Date.now() * Math.random());
console.log(myRnId()); // any random number included timeStamp;
function Myrand(max,min){
arr=[];
for (i = 0; i < max; i++) {
x = Math.floor( Math.random() * max) + min;
if(arr.includes(x) == true){
i=i-1;
}else{
if(x>max==false){
arr.push(x);
}
}
}
return arr;
}
console.log(Myrand(5,1));
Try this:
var numbers = []; // new empty array
var min, max, r, n, p;
min = 1;
max = 50;
r = 5; // how many numbers you want to extract
for (let i = 0; i < r; i++) {
do {
n = Math.floor(Math.random() * (max - min + 1)) + min;
p = numbers.includes(n);
if(!p){
numbers.push(n);
}
}
while(p);
}
console.log(numbers.join(" - "));
let arr = [];
do {
let num = Math.floor(Math.random() * 10 + 1);
arr.push(num);
arr = arr.filter((item, index) => {
return arr.indexOf(item) === index
});
} while (arr.length < 10);
console.log(arr);
HTML
<p id="array_number" style="font-size: 25px; text-align: center;"></p>
JS
var min = 1;
var max = 90;
var stop = 6; //Number of numbers to extract
var numbers = [];
for (let i = 0; i < stop; i++) {
var n = Math.floor(Math.random() * max) + min;
var check = numbers.includes(n);
if(check === false) {
numbers.push(n);
} else {
while(check === true){
n = Math.floor(Math.random() * max) + min;
check = numbers.includes(n);
if(check === false){
numbers.push(n);
}
}
}
}
sort();
//Sort the array in ascending order
function sort() {
numbers.sort(function(a, b){return a-b});
document.getElementById("array_number").innerHTML = numbers.join(" - ");
}
DEMO
The issue is that as you approach saturation you begin to take longer and longer to generate a unique number "randomly". For instance, in the example you provided above the max is 10. Once the used number array contains 8 numbers it can potentially take a long time for the 9th and 10th to be found. This is probably where the maximum call stack error is being generated.
jsFiddle Demo showing iteration count being maxed
By iterating inside of your recursion, you can see that a large amount of execution occurs when the array is completely saturated, but the function is called. In this scenario, the function should exit.
jsFiddle Demo with early break
if( used.length >= max ) return undefined;
And one last way to accomplish both the iteration checks and the infinite recursion would be like this jsFiddle Demo:
function randomNum(max, used, calls){
if( calls == void 0 ) calls = 0;
if( calls++ > 10000 ) return undefined;
if( used.length >= max ) return undefined;
var newNum = Math.floor(Math.random() * max + 1);
if($.inArray(newNum, used) === -1){
return newNum;
}else{
return randomNum(max,used,calls);
}
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>
<p id="demo"></p>
<script>
var storeArray = []
function callRamdom(){
var randomNumber = Math.floor(Math.random() * 5);
return randomNumber;
}
function randomStore(){
var localValue = callRamdom()
var status = false;
for(i=0;i<5; i++){
var aa = storeArray[i];
if(aa!=localValue){
console.log(storeArray[i]+"hhhhh"+ localValue);
if(i==4){
status=true;
}
}
else
break;
}
if(status==true){
storeArray.push(localValue);
}
if(storeArray.length!=5){
randomStore();
}
return storeArray;
}
document.getElementById("demo").innerHTML = randomStore();
</script>
</body>
</html>
while(randArr.length < SIZEOFARRAY){
val = Math.floor((Math.random() * RANGEOFVALUES));
if(randArr.indexOf(val) < 0){
randArr.push(val);
}
}
You can change SIZEOFARRAY to the size of the array you wish to use
and also change RANGEOFVALUES to the range of values you wish to randomize
const GenerateRandomNumbers = (max) => {
let orderNumbers = new Set();
for (let i = 1; ;i++){
let random = Math.floor(Math.random() * max + 1) ;
orderNumbers.add(random);
if (orderNumbers.size == max){
break;
}
}
return orderNumbers;}
function randomNumbers(max) {
function range(upTo) {
var result = [];
for(var i = 0; i < upTo; i++) result.push(i);
return result;
}
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
var myArr = shuffle(range(max));
return function() {
return myArr.shift();
};
}
Built a little test, try this on jsfiddle:
var randoms = randomNumbers(10),
rand = randoms(),
result = [];
while(rand != null) {
result.push(rand);
rand = randoms();
}
console.log(result);
Shuffle function courtesy of dzone.com.
This is how I achieve it using underscore.js
To get n integers from min to max values. Where n is the size argument.
var randomNonRepeatingIntFromInterval = function(min, max, size) {
var values = [];
while (values.length < size) {
values.push(Math.floor(Math.random() * ( max - min + 1) + min));
values = _.uniq(values);
}
return values;
}
Sorry this is a new answer to an old question, but this can be done more efficiently with a map. What you're after is random selection rather than non-repeating random. Non-repeating random is nonsensical.
Where _a is the collection, and r is not part of the collection, we lambda the random value r:
function aRandom(f){
var r = Math.random();
aRandom._a[r] ? aRandom(f) : f(r,aRandom._a[r] = 1);
}
aRandom._a = {};
//usage:
aRandom(function(r){ console.log(r) });
Redefine aRandom._a when the browser gets sluggish. To avoid eventual sluggishness, one should really use an UUID generation algo with sufficient entropy so that chances of repeat are effectively zero, rather than brute forcing differentiability. I chose the function name aRandom because latin prefix A- means "away from." Since the more it's used, the further away from random the output. The function produces one million unique values in 2100 ms on a Macbook.
Advantage of the above solution is no need to limit the set. As well, multiple callers can use it at the same time and assume their values are different from all other callers. This is handy for such things as noise jittering distributions with insured no overlaps.
However, it can be modified to return integers as well, so as to restrict ram use to the length supplied:
function aRandom(f,c){
var r = Math.floor(Math.random()*c);
aRandom._a[r] ? aRandom(f,c) : f(r,aRandom._a[r] = 1);
}
aRandom._a = {};
//usage:
var len = 10;
var resultset = [];
for(var i =0; i< len; i++){
aRandom(function(r){ resultset.push(r); }, len);
}
console.log(resultset);
randojs.com makes this a simple one-liner:
randoSequence(1, 10)
This will return an array of numbers from 1 through 10 in random order.
You just need to add the following 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 as I've shown here.
<script src="https://randojs.com/1.0.0.js"></script>
Just one solution for reference
const fiveNums = () => {
const ranNum = () => Math.floor(Math.random() * (10 + 1));
let current;
let arr = [];
while(arr.length < 5) {
if(arr.indexOf(current = ranNum()) === -1) {
arr.push(current);
}
}
return arr;
};
fiveNums();
In case no permutation is wanted and/or length shall be variable, here is a solution for non repeating randomized lists/arrays without if-statements:
Shuffle function:
Input:
Array or object(list) of arbitrary length
optional: last key to be filtered (Array: index number, List: String of key)
Output:
random Key
to get your random item use myArrayOrList[key]
// no repeat if old_key is provided
function myShuffle(arr_or_list, old_key = false) {
var keys = Array.from(Object.keys(arr_or_list)); //extracts keys
if (old_key != false) {
keys.splice(keys.indexOf(old_key), 1); // removes old_key from keys
};
var randomKey = keys[Math.floor(Math.random() * keys.length)]; // get random key
return randomKey;
}
//test:
const a = [10, 20, 30, 40, 50, 60];
const b = {
"a": 10,
"bla-bla bla": 20,
"d": 30,
"c": 40
};
var oldKeys_a = [];
var oldKeys_b = [];
oldKeys_a[0] = myShuffle(a);
oldKeys_b[0] = myShuffle(b);
var i;
for (i = 1; i < 10; i++) {
oldKeys_a[i] = myShuffle(a, oldKeys_a[i - 1]);
oldKeys_b[i] = myShuffle(b, oldKeys_b[i - 1]);
}
alert('oldKeys_a: ' + oldKeys_a + '; oldKeys_b: ' + oldKeys_b)
//random...
//>>> oldKeys_a: 1,3,0,0,5,0,4,5,2,3; oldKeys_b: d,a,d,bla-bla bla,a,c,d,bla-bla bla,a,d <<<
Non-repeating range random number generation with the recursive patterns.
const getRandom = (max, memo) => {
if (max != memo.length) {
const pos = Math.floor(Math.random() * max);
if (memo.includes(pos)) {
return getRandom(max, memo);
} else {
return pos;
}
}
}
const random = [];
const range = 6;
for (let index = 0; index < range; index++) {
random.push(getRandom(range, random))
}
console.log('random', random) // random (6) [5, 3, 0, 2, 1, 4]
function getRandomNumberNoRepeat(length){
let numberPick = [0,1,2,3,4,5,6,7,8,9]
return numberPick.sort(() => Math.random() -0.5).slice(0, length)}
console.log(getRandomNumberNoRepeat(3));
let display = document.getElementById("container");
let myArray = [];
let randomiser = (min, max, vals) => {
while (myArray.length < vals) {
let randNum = Math.floor(Math.random() * (max - min + 1) + min);
if (!myArray.includes(randNum)) {
myArray.push(randNum);
}
}
return (display.textContent = myArray.join(" - "));
};
randomiser(1, 35, 7);
Here is one where you can specify how many number you need.
I decided to write this code and make a chrome extension for it to use when playing the lotto. haha. Just a learning experience for me and thanks to all who contributed. I read all posts and I'm better of today than I was yesterday in understanding shuffle and random non repeating numbers.
You don't really want a lost of random numbers. Truly random numbers must be able to repeat.
Truly random number are like throwing dice. Any number can come up next.
Shuffled numbers are like drawing playing cards. Each number can come up only once.
What you are really asking for is to shuffle a list of numbers and then use the first so many numbers from the shuffled list.
Think of making a list of numbers in order, and then using the random number generator to randomly select a number from a copy of that list. Each time, put the selected number at the end of your new list and remove it from the copy of the old list, shortening that list. When you are done, the new list will contain the shuffled numbers and the copy of the old list will be empty.
Alternately, you can take the number selected and use it immediately, shortening the copy of the list by removing the used number. Because you have removed the number from the list, it can't come up again.

Get Random unique variable num non-repeating previous or next In javascript [duplicate]

I have the following function
function randomNum(max, used){
newNum = Math.floor(Math.random() * max + 1);
if($.inArray(newNum, used) === -1){
console.log(newNum + " is not in array");
return newNum;
}else{
return randomNum(max,used);
}
}
Basically I am creating a random number between 1 - 10 and checking to see if that number has already been created, by adding it to an array and checking the new created number against it. I call it by adding it to a variable..
UPDATED:
for(var i=0;i < 10;i++){
randNum = randomNum(10, usedNums);
usedNums.push(randNum);
//do something with ranNum
}
This works, but in Chrome I get the following error:
Uncaught RangeError: Maximum call stack size exceeded
Which I guess it's because I am calling the function inside itself too many times. Which means my code is no good.
Can someone help me with the logic? what's a best way to make sure my numbers are not repeating?
If I understand right then you're just looking for a permutation (i.e. the numbers randomised with no repeats) of the numbers 1-10?
Maybe try generating a randomised list of those numbers, once, at the start, and then just working your way through those?
This will calculate a random permutation of the numbers in nums:
var nums = [1,2,3,4,5,6,7,8,9,10],
ranNums = [],
i = nums.length,
j = 0;
while (i--) {
j = Math.floor(Math.random() * (i+1));
ranNums.push(nums[j]);
nums.splice(j,1);
}
So, for example, if you were looking for random numbers between 1 - 20 that were also even, then you could use:
nums = [2,4,6,8,10,12,14,16,18,20];
Then just read through ranNums in order to recall the random numbers.
This runs no risk of it taking increasingly longer to find unused numbers, as you were finding in your approach.
EDIT: After reading this and running a test on jsperf, it seems like a much better way of doing this is a Fisher–Yates Shuffle:
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10]);
Basically, it's more efficient by avoiding the use of 'expensive' array operations.
BONUS EDIT: Another possibility is using generators (assuming you have support):
function* shuffle(array) {
var i = array.length;
while (i--) {
yield array.splice(Math.floor(Math.random() * (i+1)), 1)[0];
}
}
Then to use:
var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10]);
ranNums.next().value; // first random number from array
ranNums.next().value; // second random number from array
ranNums.next().value; // etc.
where ranNums.next().value will eventually evaluate to undefined once you've run through all the elements in the shuffled array.
Overall this won't be as efficient as the Fisher–Yates Shuffle because you're still splice-ing an array. But the difference is that you're now doing that work only when you need it rather than doing it all upfront, so depending upon your use case, this might be better.
//random number without repetition in JavaScript, Just in one line;
//it can be used as _id;
//it not need to store or check;
const myRnId = () => parseInt(Date.now() * Math.random());
console.log(myRnId()); // any random number included timeStamp;
function Myrand(max,min){
arr=[];
for (i = 0; i < max; i++) {
x = Math.floor( Math.random() * max) + min;
if(arr.includes(x) == true){
i=i-1;
}else{
if(x>max==false){
arr.push(x);
}
}
}
return arr;
}
console.log(Myrand(5,1));
Try this:
var numbers = []; // new empty array
var min, max, r, n, p;
min = 1;
max = 50;
r = 5; // how many numbers you want to extract
for (let i = 0; i < r; i++) {
do {
n = Math.floor(Math.random() * (max - min + 1)) + min;
p = numbers.includes(n);
if(!p){
numbers.push(n);
}
}
while(p);
}
console.log(numbers.join(" - "));
let arr = [];
do {
let num = Math.floor(Math.random() * 10 + 1);
arr.push(num);
arr = arr.filter((item, index) => {
return arr.indexOf(item) === index
});
} while (arr.length < 10);
console.log(arr);
HTML
<p id="array_number" style="font-size: 25px; text-align: center;"></p>
JS
var min = 1;
var max = 90;
var stop = 6; //Number of numbers to extract
var numbers = [];
for (let i = 0; i < stop; i++) {
var n = Math.floor(Math.random() * max) + min;
var check = numbers.includes(n);
if(check === false) {
numbers.push(n);
} else {
while(check === true){
n = Math.floor(Math.random() * max) + min;
check = numbers.includes(n);
if(check === false){
numbers.push(n);
}
}
}
}
sort();
//Sort the array in ascending order
function sort() {
numbers.sort(function(a, b){return a-b});
document.getElementById("array_number").innerHTML = numbers.join(" - ");
}
DEMO
The issue is that as you approach saturation you begin to take longer and longer to generate a unique number "randomly". For instance, in the example you provided above the max is 10. Once the used number array contains 8 numbers it can potentially take a long time for the 9th and 10th to be found. This is probably where the maximum call stack error is being generated.
jsFiddle Demo showing iteration count being maxed
By iterating inside of your recursion, you can see that a large amount of execution occurs when the array is completely saturated, but the function is called. In this scenario, the function should exit.
jsFiddle Demo with early break
if( used.length >= max ) return undefined;
And one last way to accomplish both the iteration checks and the infinite recursion would be like this jsFiddle Demo:
function randomNum(max, used, calls){
if( calls == void 0 ) calls = 0;
if( calls++ > 10000 ) return undefined;
if( used.length >= max ) return undefined;
var newNum = Math.floor(Math.random() * max + 1);
if($.inArray(newNum, used) === -1){
return newNum;
}else{
return randomNum(max,used,calls);
}
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>
<p id="demo"></p>
<script>
var storeArray = []
function callRamdom(){
var randomNumber = Math.floor(Math.random() * 5);
return randomNumber;
}
function randomStore(){
var localValue = callRamdom()
var status = false;
for(i=0;i<5; i++){
var aa = storeArray[i];
if(aa!=localValue){
console.log(storeArray[i]+"hhhhh"+ localValue);
if(i==4){
status=true;
}
}
else
break;
}
if(status==true){
storeArray.push(localValue);
}
if(storeArray.length!=5){
randomStore();
}
return storeArray;
}
document.getElementById("demo").innerHTML = randomStore();
</script>
</body>
</html>
while(randArr.length < SIZEOFARRAY){
val = Math.floor((Math.random() * RANGEOFVALUES));
if(randArr.indexOf(val) < 0){
randArr.push(val);
}
}
You can change SIZEOFARRAY to the size of the array you wish to use
and also change RANGEOFVALUES to the range of values you wish to randomize
const GenerateRandomNumbers = (max) => {
let orderNumbers = new Set();
for (let i = 1; ;i++){
let random = Math.floor(Math.random() * max + 1) ;
orderNumbers.add(random);
if (orderNumbers.size == max){
break;
}
}
return orderNumbers;}
function randomNumbers(max) {
function range(upTo) {
var result = [];
for(var i = 0; i < upTo; i++) result.push(i);
return result;
}
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
var myArr = shuffle(range(max));
return function() {
return myArr.shift();
};
}
Built a little test, try this on jsfiddle:
var randoms = randomNumbers(10),
rand = randoms(),
result = [];
while(rand != null) {
result.push(rand);
rand = randoms();
}
console.log(result);
Shuffle function courtesy of dzone.com.
This is how I achieve it using underscore.js
To get n integers from min to max values. Where n is the size argument.
var randomNonRepeatingIntFromInterval = function(min, max, size) {
var values = [];
while (values.length < size) {
values.push(Math.floor(Math.random() * ( max - min + 1) + min));
values = _.uniq(values);
}
return values;
}
Sorry this is a new answer to an old question, but this can be done more efficiently with a map. What you're after is random selection rather than non-repeating random. Non-repeating random is nonsensical.
Where _a is the collection, and r is not part of the collection, we lambda the random value r:
function aRandom(f){
var r = Math.random();
aRandom._a[r] ? aRandom(f) : f(r,aRandom._a[r] = 1);
}
aRandom._a = {};
//usage:
aRandom(function(r){ console.log(r) });
Redefine aRandom._a when the browser gets sluggish. To avoid eventual sluggishness, one should really use an UUID generation algo with sufficient entropy so that chances of repeat are effectively zero, rather than brute forcing differentiability. I chose the function name aRandom because latin prefix A- means "away from." Since the more it's used, the further away from random the output. The function produces one million unique values in 2100 ms on a Macbook.
Advantage of the above solution is no need to limit the set. As well, multiple callers can use it at the same time and assume their values are different from all other callers. This is handy for such things as noise jittering distributions with insured no overlaps.
However, it can be modified to return integers as well, so as to restrict ram use to the length supplied:
function aRandom(f,c){
var r = Math.floor(Math.random()*c);
aRandom._a[r] ? aRandom(f,c) : f(r,aRandom._a[r] = 1);
}
aRandom._a = {};
//usage:
var len = 10;
var resultset = [];
for(var i =0; i< len; i++){
aRandom(function(r){ resultset.push(r); }, len);
}
console.log(resultset);
randojs.com makes this a simple one-liner:
randoSequence(1, 10)
This will return an array of numbers from 1 through 10 in random order.
You just need to add the following 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 as I've shown here.
<script src="https://randojs.com/1.0.0.js"></script>
Just one solution for reference
const fiveNums = () => {
const ranNum = () => Math.floor(Math.random() * (10 + 1));
let current;
let arr = [];
while(arr.length < 5) {
if(arr.indexOf(current = ranNum()) === -1) {
arr.push(current);
}
}
return arr;
};
fiveNums();
In case no permutation is wanted and/or length shall be variable, here is a solution for non repeating randomized lists/arrays without if-statements:
Shuffle function:
Input:
Array or object(list) of arbitrary length
optional: last key to be filtered (Array: index number, List: String of key)
Output:
random Key
to get your random item use myArrayOrList[key]
// no repeat if old_key is provided
function myShuffle(arr_or_list, old_key = false) {
var keys = Array.from(Object.keys(arr_or_list)); //extracts keys
if (old_key != false) {
keys.splice(keys.indexOf(old_key), 1); // removes old_key from keys
};
var randomKey = keys[Math.floor(Math.random() * keys.length)]; // get random key
return randomKey;
}
//test:
const a = [10, 20, 30, 40, 50, 60];
const b = {
"a": 10,
"bla-bla bla": 20,
"d": 30,
"c": 40
};
var oldKeys_a = [];
var oldKeys_b = [];
oldKeys_a[0] = myShuffle(a);
oldKeys_b[0] = myShuffle(b);
var i;
for (i = 1; i < 10; i++) {
oldKeys_a[i] = myShuffle(a, oldKeys_a[i - 1]);
oldKeys_b[i] = myShuffle(b, oldKeys_b[i - 1]);
}
alert('oldKeys_a: ' + oldKeys_a + '; oldKeys_b: ' + oldKeys_b)
//random...
//>>> oldKeys_a: 1,3,0,0,5,0,4,5,2,3; oldKeys_b: d,a,d,bla-bla bla,a,c,d,bla-bla bla,a,d <<<
Non-repeating range random number generation with the recursive patterns.
const getRandom = (max, memo) => {
if (max != memo.length) {
const pos = Math.floor(Math.random() * max);
if (memo.includes(pos)) {
return getRandom(max, memo);
} else {
return pos;
}
}
}
const random = [];
const range = 6;
for (let index = 0; index < range; index++) {
random.push(getRandom(range, random))
}
console.log('random', random) // random (6) [5, 3, 0, 2, 1, 4]
function getRandomNumberNoRepeat(length){
let numberPick = [0,1,2,3,4,5,6,7,8,9]
return numberPick.sort(() => Math.random() -0.5).slice(0, length)}
console.log(getRandomNumberNoRepeat(3));
let display = document.getElementById("container");
let myArray = [];
let randomiser = (min, max, vals) => {
while (myArray.length < vals) {
let randNum = Math.floor(Math.random() * (max - min + 1) + min);
if (!myArray.includes(randNum)) {
myArray.push(randNum);
}
}
return (display.textContent = myArray.join(" - "));
};
randomiser(1, 35, 7);
Here is one where you can specify how many number you need.
I decided to write this code and make a chrome extension for it to use when playing the lotto. haha. Just a learning experience for me and thanks to all who contributed. I read all posts and I'm better of today than I was yesterday in understanding shuffle and random non repeating numbers.
You don't really want a lost of random numbers. Truly random numbers must be able to repeat.
Truly random number are like throwing dice. Any number can come up next.
Shuffled numbers are like drawing playing cards. Each number can come up only once.
What you are really asking for is to shuffle a list of numbers and then use the first so many numbers from the shuffled list.
Think of making a list of numbers in order, and then using the random number generator to randomly select a number from a copy of that list. Each time, put the selected number at the end of your new list and remove it from the copy of the old list, shortening that list. When you are done, the new list will contain the shuffled numbers and the copy of the old list will be empty.
Alternately, you can take the number selected and use it immediately, shortening the copy of the list by removing the used number. Because you have removed the number from the list, it can't come up again.

Having Issue On Generating Random Numbers More Than Random Rage Number

I need to generate 30 Random Numbers between 1 to 20. I am using this code but this is making conflict since the loop number is bigger than the Random range (I guess!)
var arr1 = [];
for (var i = 0; i < 30;) {
var ran = Math.floor(Math.random() * 20) + 1;
if ( arr1.indexOf(ran) == -1)
arr1[i++] = ran;
}
can you please let me know why this is happening and how I can prevent this to create 30 random numbers?
Thanks
I created a fiddle here , have a look
https://jsbin.com/kagixi/edit?html,js,output
In first case, we are updating values by iterating over all the indices.
var list = new Array(30).fill(0);
list.forEach(function(d, index){
list[index] = Math.floor(Math.random() * 20) + 1;
});
console.log(list);
Another way of doing this is to initialize array just to loop and then simply create a new array with same length or replace the existing one as I did in this example.
var list2 = new Array(30).fill(0);
list2 = list2.map(function(){
return Math.floor(Math.random() * 20) + 1;
});
You miss the third argument on your for statement.
Try this:
var arr1 = [];
for (var i = 0; i < 30; i++) {
arr1[i] = Math.floor(Math.random() * 20) + 1;
}
Your code creates an infinite loop.
Your random numbers are between 1 and 20 but you want 30 of them in your array. The check for duplicates in the line if ( arr1.indexOf(ran) == -1) is guaranteed to return false after 20 iterations, creating an infinite loop.

Generating non-repeating random numbers in JS

I have the following function
function randomNum(max, used){
newNum = Math.floor(Math.random() * max + 1);
if($.inArray(newNum, used) === -1){
console.log(newNum + " is not in array");
return newNum;
}else{
return randomNum(max,used);
}
}
Basically I am creating a random number between 1 - 10 and checking to see if that number has already been created, by adding it to an array and checking the new created number against it. I call it by adding it to a variable..
UPDATED:
for(var i=0;i < 10;i++){
randNum = randomNum(10, usedNums);
usedNums.push(randNum);
//do something with ranNum
}
This works, but in Chrome I get the following error:
Uncaught RangeError: Maximum call stack size exceeded
Which I guess it's because I am calling the function inside itself too many times. Which means my code is no good.
Can someone help me with the logic? what's a best way to make sure my numbers are not repeating?
If I understand right then you're just looking for a permutation (i.e. the numbers randomised with no repeats) of the numbers 1-10?
Maybe try generating a randomised list of those numbers, once, at the start, and then just working your way through those?
This will calculate a random permutation of the numbers in nums:
var nums = [1,2,3,4,5,6,7,8,9,10],
ranNums = [],
i = nums.length,
j = 0;
while (i--) {
j = Math.floor(Math.random() * (i+1));
ranNums.push(nums[j]);
nums.splice(j,1);
}
So, for example, if you were looking for random numbers between 1 - 20 that were also even, then you could use:
nums = [2,4,6,8,10,12,14,16,18,20];
Then just read through ranNums in order to recall the random numbers.
This runs no risk of it taking increasingly longer to find unused numbers, as you were finding in your approach.
EDIT: After reading this and running a test on jsperf, it seems like a much better way of doing this is a Fisher–Yates Shuffle:
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i+1));
// swap randomly chosen element with current element
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10]);
Basically, it's more efficient by avoiding the use of 'expensive' array operations.
BONUS EDIT: Another possibility is using generators (assuming you have support):
function* shuffle(array) {
var i = array.length;
while (i--) {
yield array.splice(Math.floor(Math.random() * (i+1)), 1)[0];
}
}
Then to use:
var ranNums = shuffle([1,2,3,4,5,6,7,8,9,10]);
ranNums.next().value; // first random number from array
ranNums.next().value; // second random number from array
ranNums.next().value; // etc.
where ranNums.next().value will eventually evaluate to undefined once you've run through all the elements in the shuffled array.
Overall this won't be as efficient as the Fisher–Yates Shuffle because you're still splice-ing an array. But the difference is that you're now doing that work only when you need it rather than doing it all upfront, so depending upon your use case, this might be better.
//random number without repetition in JavaScript, Just in one line;
//it can be used as _id;
//it not need to store or check;
const myRnId = () => parseInt(Date.now() * Math.random());
console.log(myRnId()); // any random number included timeStamp;
function Myrand(max,min){
arr=[];
for (i = 0; i < max; i++) {
x = Math.floor( Math.random() * max) + min;
if(arr.includes(x) == true){
i=i-1;
}else{
if(x>max==false){
arr.push(x);
}
}
}
return arr;
}
console.log(Myrand(5,1));
Try this:
var numbers = []; // new empty array
var min, max, r, n, p;
min = 1;
max = 50;
r = 5; // how many numbers you want to extract
for (let i = 0; i < r; i++) {
do {
n = Math.floor(Math.random() * (max - min + 1)) + min;
p = numbers.includes(n);
if(!p){
numbers.push(n);
}
}
while(p);
}
console.log(numbers.join(" - "));
let arr = [];
do {
let num = Math.floor(Math.random() * 10 + 1);
arr.push(num);
arr = arr.filter((item, index) => {
return arr.indexOf(item) === index
});
} while (arr.length < 10);
console.log(arr);
HTML
<p id="array_number" style="font-size: 25px; text-align: center;"></p>
JS
var min = 1;
var max = 90;
var stop = 6; //Number of numbers to extract
var numbers = [];
for (let i = 0; i < stop; i++) {
var n = Math.floor(Math.random() * max) + min;
var check = numbers.includes(n);
if(check === false) {
numbers.push(n);
} else {
while(check === true){
n = Math.floor(Math.random() * max) + min;
check = numbers.includes(n);
if(check === false){
numbers.push(n);
}
}
}
}
sort();
//Sort the array in ascending order
function sort() {
numbers.sort(function(a, b){return a-b});
document.getElementById("array_number").innerHTML = numbers.join(" - ");
}
DEMO
The issue is that as you approach saturation you begin to take longer and longer to generate a unique number "randomly". For instance, in the example you provided above the max is 10. Once the used number array contains 8 numbers it can potentially take a long time for the 9th and 10th to be found. This is probably where the maximum call stack error is being generated.
jsFiddle Demo showing iteration count being maxed
By iterating inside of your recursion, you can see that a large amount of execution occurs when the array is completely saturated, but the function is called. In this scenario, the function should exit.
jsFiddle Demo with early break
if( used.length >= max ) return undefined;
And one last way to accomplish both the iteration checks and the infinite recursion would be like this jsFiddle Demo:
function randomNum(max, used, calls){
if( calls == void 0 ) calls = 0;
if( calls++ > 10000 ) return undefined;
if( used.length >= max ) return undefined;
var newNum = Math.floor(Math.random() * max + 1);
if($.inArray(newNum, used) === -1){
return newNum;
}else{
return randomNum(max,used,calls);
}
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>
<p id="demo"></p>
<script>
var storeArray = []
function callRamdom(){
var randomNumber = Math.floor(Math.random() * 5);
return randomNumber;
}
function randomStore(){
var localValue = callRamdom()
var status = false;
for(i=0;i<5; i++){
var aa = storeArray[i];
if(aa!=localValue){
console.log(storeArray[i]+"hhhhh"+ localValue);
if(i==4){
status=true;
}
}
else
break;
}
if(status==true){
storeArray.push(localValue);
}
if(storeArray.length!=5){
randomStore();
}
return storeArray;
}
document.getElementById("demo").innerHTML = randomStore();
</script>
</body>
</html>
while(randArr.length < SIZEOFARRAY){
val = Math.floor((Math.random() * RANGEOFVALUES));
if(randArr.indexOf(val) < 0){
randArr.push(val);
}
}
You can change SIZEOFARRAY to the size of the array you wish to use
and also change RANGEOFVALUES to the range of values you wish to randomize
const GenerateRandomNumbers = (max) => {
let orderNumbers = new Set();
for (let i = 1; ;i++){
let random = Math.floor(Math.random() * max + 1) ;
orderNumbers.add(random);
if (orderNumbers.size == max){
break;
}
}
return orderNumbers;}
function randomNumbers(max) {
function range(upTo) {
var result = [];
for(var i = 0; i < upTo; i++) result.push(i);
return result;
}
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
var myArr = shuffle(range(max));
return function() {
return myArr.shift();
};
}
Built a little test, try this on jsfiddle:
var randoms = randomNumbers(10),
rand = randoms(),
result = [];
while(rand != null) {
result.push(rand);
rand = randoms();
}
console.log(result);
Shuffle function courtesy of dzone.com.
This is how I achieve it using underscore.js
To get n integers from min to max values. Where n is the size argument.
var randomNonRepeatingIntFromInterval = function(min, max, size) {
var values = [];
while (values.length < size) {
values.push(Math.floor(Math.random() * ( max - min + 1) + min));
values = _.uniq(values);
}
return values;
}
Sorry this is a new answer to an old question, but this can be done more efficiently with a map. What you're after is random selection rather than non-repeating random. Non-repeating random is nonsensical.
Where _a is the collection, and r is not part of the collection, we lambda the random value r:
function aRandom(f){
var r = Math.random();
aRandom._a[r] ? aRandom(f) : f(r,aRandom._a[r] = 1);
}
aRandom._a = {};
//usage:
aRandom(function(r){ console.log(r) });
Redefine aRandom._a when the browser gets sluggish. To avoid eventual sluggishness, one should really use an UUID generation algo with sufficient entropy so that chances of repeat are effectively zero, rather than brute forcing differentiability. I chose the function name aRandom because latin prefix A- means "away from." Since the more it's used, the further away from random the output. The function produces one million unique values in 2100 ms on a Macbook.
Advantage of the above solution is no need to limit the set. As well, multiple callers can use it at the same time and assume their values are different from all other callers. This is handy for such things as noise jittering distributions with insured no overlaps.
However, it can be modified to return integers as well, so as to restrict ram use to the length supplied:
function aRandom(f,c){
var r = Math.floor(Math.random()*c);
aRandom._a[r] ? aRandom(f,c) : f(r,aRandom._a[r] = 1);
}
aRandom._a = {};
//usage:
var len = 10;
var resultset = [];
for(var i =0; i< len; i++){
aRandom(function(r){ resultset.push(r); }, len);
}
console.log(resultset);
randojs.com makes this a simple one-liner:
randoSequence(1, 10)
This will return an array of numbers from 1 through 10 in random order.
You just need to add the following 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 as I've shown here.
<script src="https://randojs.com/1.0.0.js"></script>
Just one solution for reference
const fiveNums = () => {
const ranNum = () => Math.floor(Math.random() * (10 + 1));
let current;
let arr = [];
while(arr.length < 5) {
if(arr.indexOf(current = ranNum()) === -1) {
arr.push(current);
}
}
return arr;
};
fiveNums();
In case no permutation is wanted and/or length shall be variable, here is a solution for non repeating randomized lists/arrays without if-statements:
Shuffle function:
Input:
Array or object(list) of arbitrary length
optional: last key to be filtered (Array: index number, List: String of key)
Output:
random Key
to get your random item use myArrayOrList[key]
// no repeat if old_key is provided
function myShuffle(arr_or_list, old_key = false) {
var keys = Array.from(Object.keys(arr_or_list)); //extracts keys
if (old_key != false) {
keys.splice(keys.indexOf(old_key), 1); // removes old_key from keys
};
var randomKey = keys[Math.floor(Math.random() * keys.length)]; // get random key
return randomKey;
}
//test:
const a = [10, 20, 30, 40, 50, 60];
const b = {
"a": 10,
"bla-bla bla": 20,
"d": 30,
"c": 40
};
var oldKeys_a = [];
var oldKeys_b = [];
oldKeys_a[0] = myShuffle(a);
oldKeys_b[0] = myShuffle(b);
var i;
for (i = 1; i < 10; i++) {
oldKeys_a[i] = myShuffle(a, oldKeys_a[i - 1]);
oldKeys_b[i] = myShuffle(b, oldKeys_b[i - 1]);
}
alert('oldKeys_a: ' + oldKeys_a + '; oldKeys_b: ' + oldKeys_b)
//random...
//>>> oldKeys_a: 1,3,0,0,5,0,4,5,2,3; oldKeys_b: d,a,d,bla-bla bla,a,c,d,bla-bla bla,a,d <<<
Non-repeating range random number generation with the recursive patterns.
const getRandom = (max, memo) => {
if (max != memo.length) {
const pos = Math.floor(Math.random() * max);
if (memo.includes(pos)) {
return getRandom(max, memo);
} else {
return pos;
}
}
}
const random = [];
const range = 6;
for (let index = 0; index < range; index++) {
random.push(getRandom(range, random))
}
console.log('random', random) // random (6) [5, 3, 0, 2, 1, 4]
function getRandomNumberNoRepeat(length){
let numberPick = [0,1,2,3,4,5,6,7,8,9]
return numberPick.sort(() => Math.random() -0.5).slice(0, length)}
console.log(getRandomNumberNoRepeat(3));
let display = document.getElementById("container");
let myArray = [];
let randomiser = (min, max, vals) => {
while (myArray.length < vals) {
let randNum = Math.floor(Math.random() * (max - min + 1) + min);
if (!myArray.includes(randNum)) {
myArray.push(randNum);
}
}
return (display.textContent = myArray.join(" - "));
};
randomiser(1, 35, 7);
Here is one where you can specify how many number you need.
I decided to write this code and make a chrome extension for it to use when playing the lotto. haha. Just a learning experience for me and thanks to all who contributed. I read all posts and I'm better of today than I was yesterday in understanding shuffle and random non repeating numbers.
You don't really want a lost of random numbers. Truly random numbers must be able to repeat.
Truly random number are like throwing dice. Any number can come up next.
Shuffled numbers are like drawing playing cards. Each number can come up only once.
What you are really asking for is to shuffle a list of numbers and then use the first so many numbers from the shuffled list.
Think of making a list of numbers in order, and then using the random number generator to randomly select a number from a copy of that list. Each time, put the selected number at the end of your new list and remove it from the copy of the old list, shortening that list. When you are done, the new list will contain the shuffled numbers and the copy of the old list will be empty.
Alternately, you can take the number selected and use it immediately, shortening the copy of the list by removing the used number. Because you have removed the number from the list, it can't come up again.

Javascript generate random unique number every time

Ok so i need to create four randomly generated numbers between 1-10 and they cannot be the same. so my thought is to add each number to an array but how can I check to see if the number is in the array, and if it is, re-generate the number and if it isnt add the new number to the array?
so basically it will go,
1.create new number and add to array
2.create second new number, check to see if it exist already, if it doesn't exist, add to array. If it does exist, re-create new number, check again etc...
3.same as above and so on.
You want what is called a 'random grab bag'. Consider you have a 'bag' of numbers, each number is only represented once in this bag. You take the numbers out, at random, for as many as you need.
The problem with some of the other solutions presented here is that they randomly generate the number, and check to see if it was already used. This will take longer and longer to complete (theoretically up to an infinite amount of time) because you are waiting for the random() function to return a value you don't already have (and it doesn't have to do that, it could give you 1-9 forever, but never return 10).
There are a lot of ways to implement a grab-bag type solution, each with varying degrees of cost (though, if done correctly, won't ever be infinite).
The most basic solution to your problem would be the following:
var grabBag = [1,2,3,4,5,6,7,8,9,10];
// randomize order of elements with a sort function that randomly returns -1/0/1
grabBag.sort(function(xx,yy){ return Math.floor(Math.random() * 3) - 1; })
function getNextRandom(){
return grabBag.shift();
};
var originalLength = grabBag.length;
for(var i = 0; i < originalLength; i++){
console.log(getNextRandom());
}
This is of course destructive to the original grabBag array. And I'm not sure how 'truly random' that sort is, but for many applications it could be 'good enough'.
An slightly different approach would be to store all the unused elements in an array, randomly select an index, and then remove the element at that index. The cost here is how frequently you are creating/destroying arrays each time you remove an element.
Here are a couple versions using Matt's grabBag technique:
function getRandoms(numPicks) {
var nums = [1,2,3,4,5,6,7,8,9,10];
var selections = [];
// randomly pick one from the array
for (var i = 0; i < numPicks; i++) {
var index = Math.floor(Math.random() * nums.length);
selections.push(nums[index]);
nums.splice(index, 1);
}
return(selections);
}
You can see it work here: http://jsfiddle.net/jfriend00/b3MF3/.
And, here's a version that lets you pass in the range you want to cover:
function getRandoms(numPicks, low, high) {
var len = high - low + 1;
var nums = new Array(len);
var selections = [], i;
// initialize the array
for (i = 0; i < len; i++) {
nums[i] = i + low;
}
// randomly pick one from the array
for (var i = 0; i < numPicks; i++) {
var index = Math.floor(Math.random() * nums.length);
selections.push(nums[index]);
nums.splice(index, 1);
}
return(selections);
}
And a fiddle for that one: http://jsfiddle.net/jfriend00/UXnGB/
Use an array to see if the number has already been generated.
var randomArr = [], trackingArr = [],
targetCount = 4, currentCount = 0,
min = 1, max = 10,
rnd;
while (currentCount < targetCount) {
rnd = Math.floor(Math.random() * (max - min + 1)) + min;
if (!trackingArr[rnd]) {
trackingArr[rnd] = rnd;
randomArr[currentCount] = rnd;
currentCount += 1;
}
}
alert(randomArr); // Will contain four unique, random numbers between 1 and 10.
Working example: http://jsfiddle.net/FishBasketGordo/J4Ly7/
var a = [];
for (var i = 0; i < 5; i++) {
var r = Math.floor(Math.random()*10) + 1;
if(!(r in a))
a.push(r);
else
i--;
}
That'll do it for you. But be careful. If you make the number of random numbers generated greater than the may number (10) you'll hit an infinite loop.
I'm using a recursive function. The test function pick 6 unique value between 1 and 9.
//test(1, 9, 6);
function test(min, max, nbValue){
var result = recursValue(min, max, nbValue, []);
alert(result);
}
function recursValue(min, max, nbValue, result){
var randomNum = Math.random() * (max-min);
randomNum = Math.round(randomNum) + min;
if(!in_array(randomNum, result)){
result.push(randomNum);
nbValue--;
}
if(nbValue>0){
recursValue(min, max, nbValue, result);
}
return result;
}
function in_array(value, my_array){
for(var i=0;i< my_array.length; i++){
if(my_array[i] == value){
console.log(my_array+" val "+value);
return true;
}
}
return false;
}
Here is a recursive function what are you looking for.
"howMany" parameter is count of how many unique numbers you want to generate.
"randomize" parameter is biggest number that function can generate.
for example : rand(4,8) function returns an array that has 4 number in it, and the numbers are between 0 and 7 ( because as you know, Math.random() function generates numbers starting from zero to [given number - 1])
var array = [];
var isMatch= false;
function rand(howMany, randomize){
if( array.length < howMany){
var r = Math.floor( Math.random() * randomize );
for( var i = 0; i < howMany; i++ ){
if( array[i] !== r ){
isMatch= false;
continue;
} else {
isMatch= true;
break;
}
}
if( isMatch == false ){
array.push(r);
ran(howMany, randomize);
}
ran(howMany, randomize);
return array;
}
}
In your answer earlier, you do have a small bug. Instead of
var originalLength = grabBag.length;
for(var i = 0; i < originalLength .length; i++){
console.log(getNextRandom());
}
I believe you meant:
var originalLength = grabBag.length;
for(var i = 0; i < originalLength; i++){
console.log(getNextRandom());
}
Thanks.

Categories

Resources