Skip to content

Instantly share code, notes, and snippets.

@jdfm
Last active January 4, 2016 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdfm/47c1ab60fad764dd8a1d to your computer and use it in GitHub Desktop.
Save jdfm/47c1ab60fad764dd8a1d to your computer and use it in GitHub Desktop.
Generating random numbers with javascript
/**
* Generate an integer between [min..max]
*
* @param {Number} min The minimum integer you want to create
* @param {Number} max The maximum integer you want to create
* @return {Number} A random integer between [min..max]
*/
function getRandomInteger(min, max){
return min + Math.floor(Math.random() * (max - min + 1));
}
/**
* Generate an integer between [min..max] without pivot
*
* @param {Number} min The minimum integer you want to create
* @param {Number} max The maximum integer you want to create
* @param {Number} pivot An integer in [min..max] you don't want to create
* @return {Number} An integer in [min..max]-[pivot]
*/
function getPivottedRandomInteger(min, max, pivot){
return min + (pivot - min + 1 + Math.floor(Math.random() * (max - min))) % (max - min + 1);
}
/**
* Swap values in an object
*
* @param {Object} obj The object whose values you wish to swap
* @param {Number} i The index of the first element you wish to swap
* @param {Number} j The index of the second element you wish to swap
*/
function swap(obj, i, j){
var temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
/**
* Shuffles the elements of an array
*
* @param {Array} list The list whose elements you wish to shuffle
* @return {Array} The shuffled array
*/
function shuffleList(list){
var i = list.length;
while(i--){
swap(list, i, getRandomInteger(0, i));
}
return list;
}
/**
* Create a range of numbers between [min..max] inclusive
*
* @param {Number} min Your minimum integer, must be smaller than max
* @param {Number} max Your maximum integer, must be larger than min
* @return {Array} Range of values [min..max]
*/
function createRange(min, max){
var length = max - min + 1;
var range = new Array(length);
while(length--){
range[length] = length + min;
}
return range;
}
/**
* Make list of non-repeating integers between [randomMin..randomMax] of size listSize
*
* O(listSize) space complexity
* O(listSize) to O(infinity) run time, maybe your random number generator sucks, I don't know
*
* @param {Number} randomMin Minimum possible random integer
* @param {Number} randomMax Maximum possible random integer
* @param {Number} listSize Size of the list final list
* @return {Array} List of size listSize of non-repeating random
* integers between [randomMin..randomMax]
*/
function createRandomList(randomMin, randomMax, listSize){
var randomRangeSize = randomMax - randomMin + 1;
// invalid parameters result in an empty array
if (randomRangeSize < listSize){
return [];
}
// if the ammount of memory we're going to use isn't less than what we will
// be creating with the alternative algorithm, just use the naive approach
if(randomRangeSize <= 2 * listSize){
var output = shuffleList(createRange(randomMin, randomMax));
if(randomRangeSize === listSize){
return output;
}
return output.splice(0, listSize);
}
// the catch with this algorithm is that, given the random nature of the
// problem, it might repeat a few steps in the process until we have a list
// of size listSize, there is no guarantee that the steps will not repeat
// indefinitely
var hash = {}, output = new Array(listSize), i = listSize, currentInteger;
while(i){
currentInteger = getRandomInteger(randomMin, randomMax);
if(hash[currentInteger] === true){ continue; }
hash[currentInteger] = true;
output[i - 1] = currentInteger;
i--;
}
return output;
}
@jdfm
Copy link
Author

jdfm commented Dec 22, 2015

Just a collection of utilities that either generate or use random numbers.

@jdfm
Copy link
Author

jdfm commented Jan 4, 2016

Examples of how getPivottedRandomInteger works:

min: 2
max: 6
size: 5
initialRange: [ pivot - min + 1, ... , max + pivot - 2 * min ]

pivot: 2
desiredRange: [3, 4, 5, 6]

[ 1, 2, 3, 4 ]
[ 1, 2, 3, 4 ] % size
[ 1, 2, 3, 4 ] + min
[ 3, 4, 5, 6 ]

pivot: 3
desiredRange: [4, 5, 6, 2]

[ 2, 3, 4, 5 ]
[ 2, 3, 4, 5 ] % size
[ 2, 3, 4, 0 ] + min
[ 4, 5, 6, 2 ]

pivot: 4
desiredRange: [5, 6, 2, 3]

[ 3, 4, 5, 6 ]
[ 3, 4, 5, 6 ] % size
[ 3, 4, 0, 1 ] + min
[ 5, 6, 2, 3 ]

pivot: 5
desiredRange: [6, 2, 3, 4]

[ 4, 5, 6, 7 ]
[ 4, 5, 6, 7 ] % size
[ 4, 0, 1, 2 ] + min
[ 6, 2, 3, 4 ]

pivot: 6
desiredRange: [2, 3, 4, 5]

[ 5, 6, 7, 8 ]
[ 5, 6, 7, 8 ] % size
[ 0, 1, 2, 3 ] + min
[ 2, 3, 4, 5 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment