Javascript error-free random number generator?

I need help writing code that will generate a random number from an array of 12 numbers and print it 9 times without cheating. It was hard for me. Any ideas?

+2
source share
6 answers
var nums = [1,2,3,4,5,6,7,8,9,10,11,12]; var gen_nums = []; function in_array(array, el) { for(var i = 0 ; i < array.length; i++) if(array[i] == el) return true; return false; } function get_rand(array) { var rand = array[Math.floor(Math.random()*array.length)]; if(!in_array(gen_nums, rand)) { gen_nums.push(rand); return rand; } return get_rand(array); } for(var i = 0; i < 9; i++) { document.write(get_rand(nums)); } 
+7
source

The most efficient and effective way to do this is to shuffle your numbers and then print the first nine of them. Use a good shuffle algorithm . As suggested by Thilo, you will get poor results. See here.

Edit Here is an example of a Knuth Shuffle algorithm:

 void shuffle(vector<int> nums) { for (int i = nums.size()-1; i >= 0; i--) { // this line is really shorthand, but gets the point across, I hope. swap(nums[i],nums[rand()%i]); } } 
+4
source

If you understand correctly, you want to shuffle your array.

Repeat a couple of times (the length of the array should do), and at each iteration get two indexes of random arrays and replace them with two elements. (Update: if you are really serious about this, this might not be the best best algorithm ).

Then you can print the first nine elements of the array, which will be in random order and not repeated.

0
source

This is relatively easy to do, the theory behind it creates another array that keeps track of which elements of the array you used.

 var tempArray = new Array(12),i,r; for (i=0;i<9;i++) { r = Math.floor(Math.random()*12); // Get a random index if (tempArray[r] === undefined) // If the index hasn't been used yet { document.write(numberArray[r]); // Display it tempArray[r] = true; // Flag it as have been used } else // Otherwise { i--; // Try again } } 

Other methods include shuffling the array, removing used elements from the array, or moving used elements to the end of the array.

0
source

Try it once:

 //Here o is the array; var testArr = [6, 7, 12, 15, 17, 20, 21]; shuffle = function(o){ //v1.0 for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; }; shuffle(testArr); 
0
source

Here is a general way to get random numbers between min and max without duplicates:

 function inArray(arr, el) { for(var i = 0 ; i < arr.length; i++) if(arr[i] == el) return true; return false; } function getRandomIntNoDuplicates(min, max, DuplicateArr) { var RandomInt = Math.floor(Math.random() * (max - min + 1)) + min; if (DuplicateArr.length > (max-min) ) return false; // break endless recursion if(!inArray(DuplicateArr, RandomInt)) { DuplicateArr.push(RandomInt); return RandomInt; } return getRandomIntNoDuplicates(min, max, DuplicateArr); //recurse } 

call with:

 var duplicates =[]; for (var i = 1; i <= 6 ; i++) { console.log(getRandomIntNoDuplicates(1,10,duplicates)); } 
0
source

All Articles