Pick random combination from 194481 possibilities - javascript

I have a file of 194481 permutations for
0,1,2,3,4,5,6,...,21
which looks like this;
[0,0,0,0],[0,0,0,1],[0,0,0,2],[0,0,0,3],[0,0,0,4],[0,0,0,5],[0,0,0,6],[0,0,0,7],[0,0,0,8],[0,0,0,9],[0,0,0,10],[0,0,0,11],[0,0,0,12],[0,0,0,13],[0,0,0,14],[0,0,0,15],[0,0,0,16],[0,0,0,17],[0,0,0,18],[0,0,0,19],[0,0,0,20],[0,0,1,0],[0,0,1,1],[0,0,1,2],[0,0,1,3],[0,0,1,4],[0,0,1,5],[0,0,1,6],[0,0,1,7],[0,0,1,8],[0,0,1,9],[0,0,1,10],[0,0,1,11],[0,0,1,12],[0,0,1,13],[0,0,1,14],[0,0,1,15],[0,0,1,16],[0,0,1,17],[0,0,1,18],[0,0,1,19],[0,0,1,20],[0,0,2,0],[0,0,2,1],[0,0,2,2],[0,0,2,3],[0,0,2,4],[0,0,2,5],[0,0,2,6],[0,0,2,7],[0,0,2,8],[0,0,2,9],[0,0,2,10],[0,0,2,11],[0,0,2,12],[0,0,2,13],[0,0,2,14],[0,0,2,15],[0,0,2,16],[0,0,2,17],[0,0,2,18],[0,0,2,19],[0,0,2,20],[0,0,3,0],[0,0,3,1],[0,0,3,2],[0,0,3,3],[0,0,3,4],[0,0,3,5],[0,0,3,6],[0,0,3,7],[0,0,3,8],[0,0,3,9],[0,0,3,10],[0,0,3,11],[0,0,3,12],[0,0,3,13],[0,0,3,14],[0,0,3,15],[0,0,3,16],[0,0,3,17],[0,0,3,18],[0,0,3,19],[0,0,3,20],[0,0,4,0],[0,0,4,1],[0,0,4,2],[0,0,4,3],[0,0,4,4],[0,0,4,5],[0,0,4,6],[0,0,4,7],[0,0,4,8],[0,0,4,9],[0,0,4,10],[0,0,4,11],[0,0,4,12],[0,0,4,13],[0,0,4,14],[0,0,4,15],[0,0,4,16],[0,0,4,17],[0,0,4,18],[0,0,4,19],[0,0,4,20],[0,0,5,0],[0,0,5,1],[0,0,5,2],[0,0,5,3],[0,0,5,4],[0,0,5,5],[0,0,5,6],[0,0,5,7],[0,0,5,8],[0,0,5,9],[0,0,5,10],[0,0,5,11],[0,0,5,12],[0,0,5,13],[0,0,5,14],[0,0,5,15],[0,0,5,16],[0,0,5,17],[0,0,5,18],[0,0,5,19],[0,0,5,20],[0,0,6,0],[0,0,6,1],[0,0,6,2],[0,0,6,3],[0,0,6,4],[0,0,6,5],[0,0,6,6],[0,0,6,7],[0,0,6,8],[0,0,6,9],[0,0,6,10],[0,0,6,11],[0,0,6,12],[0,0,6,13],[0,0,6,14],[0,0,6,15],[0,0,6,16],[0,0,6,17],[0,0,6,18],[0,0,6,19],[0,0,6,20],[0,0,7,0],[0,0,7,1],[0,0,7,2],[0,0,7,3],[0,0,7,4],[0,0,7,5],[0,0,7,6],[0,0,7,7],[0,0,7,8],[0,0,7,9],[0,0,7,10],[0,0,7,11],[0,0,7,12],[0,0,7,13],[0,0,7,14],[0,0,7,15],[0,0,7,16],[0,0,7,17],[0,0,7,18],[0,0,7,19],[0,0,7,20],[0,0,8,0],[0,0,8,1],[0,0,8,2],[0,0,8,3],[0,0,8,4],[0,0,8,5],[0,0,8,6],[0,0,8,7],[0,0,8,8],[0,0,8,9],[0,0,8,10],[0,0,8,11],[0,0,8,12],[0,0,8,13],[0,0,8,14],[0,0,8,15],[0,0,8,16],[0,0,8,17],[0,0,8,18],[0,0,8,19],[0,0,8,20],[0,0,9,0],[0,0,9,1],[0,0,9,2],[0,0,9,3],[0,0,9,4],[0,0,9,5],[0,0,9,6],[0,0,9,7],[0,0,9,8],[0,0,9,9],[0,0,9,10],[0,0,9,11],[0,0,9,12],[0,0,9,13],[0,0,9,14],[0,0,9,15],[0,0,9,16],[0,0,9,17],[0,0,9,18],[0,0,9,19],[0,0,9,20],[0,0,10,0],[0,0,10,1],[0,0,10,2],[0,0,10,3],[0,0,10,4],[0,0,10,5],[0,0,10,6],[0,0,10,7],[0,0,10,8],[0,0,10,9],[0,0,10,10],[0,0,10,11],[0,0,10,12],[0,0,10,13],[0,0,10,14],[0,0,10,15],[0,0,10,16],[0,0,10,17],[0,0,10,18],[0,0,10,19],[0,0,10,20],[0,0,11,0],[0,0,11,1],[0,0,11,2],[0,0,11,3],[0,0,11,4],[0,0,11,5],[0,0,11,6],[0,0,11,7],[0,0,11,8],[0,0,11,9],[0,0,11,10],[0,0,11,11],[0,0,11,12],[0,0,11,13],[0,0,11,14],[0,0,11,15],[0,0,11,16],[0,0,11,17],[0,0,11,18],[0,0,11,19],[0,0,11,20],[0,0,12,0],[0,0,12,1],[0,0,12,2],[0,0,12,3],[0,0,12,4],[0,0,12,5],[0,0,12,6],[0,0,12,7],[0,0,12,8],[0,0,12,9],[0,0,12,10],[0,0,12,11],[0,0,12,12],[0,0,12,13],[0,0,12,14],[0,0,12,15],[0,0,12,16],[0,0,12,17],[0,0,12,18],[0,0,12,19],[0,0,12,20],[0,0,13,0],[0,0,13,1],[0,0,13,2],[0,0,13,3],[0,0,13,4],[0,0,13,5],[0,0,13,6],[0,0,13,7],[0,0,13,8],[0,0,13,9],[0,0,13,10],[0,0,13,11],[0,0,13,12],[0,0,13,13],[0,0,13,14],[0,0,13,15],[0,0,13,16],[0,0,13,17],[0,0,13,18],[0,0,13,19],[0,0,13,20],[0,0,14,0],[0,0,14,1],[0,0,14,2],[0,0,14,3],[0,0,14,4],[0,0,14,5],[0,0,14,6],[0,0,14,7],[0,0,14,8],[0,0,14,9],[0,0,14,10],[0,0,14,11],[0,0,14,12],[0,0,14,13],[0,0,14,14],[0,0,14,15],[0,0,14,16],[0,0,14,17],[0,0,14,18],[0,0,14,19],[0,0,14,20],[0,0,15,0],[0,0,15,1],[0,0,15,2],[0,0,15,3],[0,0,15,4],[0,0,15,5],[0,0,15,6],[0,0,15,7],[0,0,15,8],[0,0,15,9],[0,0,15,10],[0,0,15,11],[0,0,15,12],[0,0,15,13],[0,0,15,14],[0,0,15,15],[0,0,15,16],[0,0,15,17],[0,0,15,18],[0,0,15,19],[0,0,15,20],[0,0,16,0],[0,0,16,1],[0,0,16,2],[0,0,16,3],[0,0,16,4],[0,0,16,5],[0,0,16,6],[0,0,16,7],[0,0,16,8],[0,0,16,9],[0,0,16,10],[0,0,16,11],[0,0,16,12],[0,0,16,13],[0,0,16,14],[0,0,16,15],[0,0,16,16],[0,0,16,17],[0,0,16,18],[0,0,16,19],[0,0,16,20],[0,0,17,0],[0,0,17,1],[0,0,17,2],[0,0,17,3],[0,0,17,4],[0,0,17,5],[0,0,17,6],[0,0,17,7],[0,0,17,8],[0,0,17,9],[0,0,17,10],[0,0,17,11],[0,0,17,12],[0,0,17,13],[0,0,17,14],[0,0,17,15],[0,0,17,16],[0,0,17,17]... etc.
It ends at [20,20,20,20].
I need to pick 50 combinations from the file and assign it to a variable so it would be like
var combinationsArr = [
[0,0,17,9],[0,0,17,10],[0,0,17,11],[0,0,17,12]
]; //BUT 50 of them
it's okay if it is just in order like [0,0,0,0],[0,0,0,1],[0,0,0,2],[0,0,0,3],[0,0,0,4],[0,0,0,5],[0,0,0,6],[0,0,0,7],[0,0,0,8],[0,0,0,9],[0,0,0,10],[0,0,0,11],[0,0,0,12] and doesn't have to be super random like [1,2,3,4],[9,12,13,15],[20,12,6,7]
as long as it is able to pick 50 of them.
I am doing this because 194481 combinations are a lot and makes my program carsh. so I just decided i'll put it in a text file and pick random points from the text file like from [0,0,0,1] to [0,0,0,50] OR
from [0,1,0,0] to [0,1,0,49] if that's possible.
because i have to generate a random combination. I have another array of combinations which are not supposed to be generated. Let's call it notAllowedArr.
var notAllowedArr = [
[0,0,17,9],[0,0,17,12]
];
I am thinking, i'll just generate 50 combinations and remove the ones listed in notAllowedArr then pick one from combinationsArr as the final result. I will still have to find code to remove those from combinationsArr but the result should be like.
var combinationsArr = [[0,0,17,10],[0,0,17,11]];
then i'll have a code to pick a random value from combinationsArr.
example. combinationsArr[0].
so the final result would be; [0,0,17,10]
Does anyone have a better solution?

If I understand correctly, you need to pick one random combination, which is not present in a list of forbidden combinations.
You can consider a combination of four numbers from 0 to 20 as a number from 0 to 194480 in base-21 notation. So instead of having to store all combinations in a file, we just pick a random number and convert it to base-21.
To choose a random number in a range where some values are forbidden, choose a number in the range from 0 to the maximum minus the number of forbidden values; then iterate over the forbidden values from small to large, and increment the random number every time you find a smaller or equal forbidden value.
This will make sure that every combination has the same probability of being chosen, and avoids the possibility of repeatedly choosing a forbidden combination.
function randomBase21(skip) {
var dec = [], result = [], num;
// CONVERT FORBIDDEN COMBINATIONS FROM BASE-21 TO DECIMAL AND SORT
for (var i = 0; i < skip.length; i++) {
dec[i] = skip[i][0] * 9261 + skip[i][1] * 441 + skip[i][2] * 21 + skip[i][3];
}
dec.sort(function(a, b){return a - b});
// GENERATE RANDOM NUMBER FROM 0 TO MAX - NUMBER OF FORBIDDEN COMBINATIONS
num = Math.floor(Math.random() * (194481 - skip.length));
// INCREMENT RANDOM NUMBER FOR EVERY SMALLER FORBIDDEN COMBINATION
for (var i = 0; i < skip.length && num >= dec[i]; i++) {
++num;
}
// CONVERT RANDOM NUMBER TO FOUR BASE-21 DIGITS
for (var i = 3; i >= 0; i--, num /= 21) {
result[i] = Math.floor(num % 21);
}
return result;
}
var notAllowed = [[0,0,17,9],[0,0,17,12],[20,19,17,12],[15,16,17,12]];
document.write(randomBase21(notAllowed));

Something like this should work (off the top of my head and not tested/debugged):
var samples = new Array();
for(var index = 0; index < 50; index++) {
samples.push(generatePermutation());
}
function generatePermutation() {
var result = [Math.floor(Math.random() * 20) + 1,
Math.floor(Math.random() * 20) + 1,
Math.floor(Math.random() * 20) + 1,
Math.floor(Math.random() * 20) + 1];
}

I just thought of a better way.
I think I will make a function that generates a random combination.
Then check if it exists in notAllowedArr. If it exists, it will generate another one. If not then that will return that combination :D
I think this will work faster ^^;

Related

How to generate a random number , the length of the number needs to be 21 and should be prefixed with "01" in Postman

I am using the code below to generate a random number. Result output should be a length of 21 and with prefix starting with "01"
postman.setGlobalVariable('RandomOrderId', "01"+ Math.floor(Math.random() * 1234567890123456789));
However, sometimes it generates output with a length of 20, 19 and so on. How do I make sure it always generate a random number with length 21 and prefix as "01"?
Math.random gives you a random number between 0 and 1, multiplying that with a million gives you something between zero and a million. If you want to allow leading numbers you could convert the number to a string and add the necessary amount of leading zeroes, if that is possible in your case. Else, you could generate 21 random numbers between 0 and 9 and combine them to a random huge number (which could also have leading zeroes).
Code could look like this:
var s = "01";
for(var i = 0; i < 21; i++) {
s = s + Math.floor(Math.random() * 10);
}
console.log(s);
Use the below
postman.setGlobalVariable('RandomOrderId', "01"+ Math.floor(1000000000000000000 + Math.random() * 1000000000000000000));
Verify using the below
function getRandomInt() {
return ( "01" + Math.floor(1000000000000000000 + Math.random() * 1000000000000000000));
}
console.log(getRandomInt());
console.log(getRandomInt());
console.log(getRandomInt());
console.log(getRandomInt());
console.log(getRandomInt());
Try injecting this JS:
var len = 21;
parseInt((Math.random() * 20 + 1) * Math.pow(20,len-1), 20);
You could also use the Lodash _.random() method within Postman. Set the lower and upper values for the range and it will return a 19 digit number between those values. The 01 prefix can be added straight to the global value.
const lowerLimit = 1000000000000000000
const upperLimit = 9999999999999999999
pm.globals.set("randomNumber", `01${_.random(lowerLimit,upperLimit)}`)
Just a different way to get the same result as the accepted answer.

Random number generator, what's wrong with my approach/statistics? [JS]

First of all, what I want to know is if I am doing a systematic fault or am I messing up with the math, or do you have any ideas what could be wrong?
I was trying to write a little random number generator which numbers can be influenced / verified by the user (provably fair). The generated number is between 4096 and 65535. I simulated the function 100,000 times and get some strange statistics (Or am I thinking wrong??).
Why is the chance that the number is under 8000 around ~50%. Shouldn't it be 50% around the middle of the number range (~30700)?
Here is the output of the simulation:
< 65536 : 100000 Times :100%
< 60000 : 91813 Times :91.813%
< 56000 : 86406 Times :86.406%
< 52000 : 81334 Times :81.334%
< 48000 : 76743 Times :76.743%
< 32768 : 62356 Times :62.356%
< 32000 : 61748 Times :61.748%
< 30719 : 60860 Times :60.86%
< 24000 : 56628 Times :56.628%
< 16000 : 52871 Times :52.871%
< 12000 : 51540 Times :51.54%
< 8000 : 50447 Times :50.447%
< 6000 : 36003 Times :36.003%
< 5096 : 21583 Times :21.583%
< 4608 : 11714 Times :11.714%
< 4250 : 3674 Times :3.674%
< 4100 : 100 Times :0.1%
< 4096 : 0 Times :0%
A little more details on the function I wrote:
I am generating two hashes. One is the userhash and the other is the serverhash. Both of these have a format like e.g.:
Server =3CDC3C8C97DEE62169B2C403BB2B6B501C1B0A0BD8015699B47DA67789C3F628
User =CF27CC73E33E0AC1DA5239DE8DAF94044D89B8636DA90F4CE510E652C8AC7F54
(The Userhash, is generated by a unique ID. The Serverhash is generated by taking a random number (standard function: math.random() )and a timestamp and then HMAC-SHA-512 them.)
To get my "random" number, i take both (user and server) hashes and add them as hexadecimal numbers (userhash + serverhash = result). Then I take the result and cut everything except the first 4 digits. (E.g result = CF27)
After that, I convert it to decimal again.
The smallest number I can get from this should be in Hex 1000 (dec=4096), the biggest in Hex FFFF (dec=65535). That means my random number should be in a Range of 4096 - 65535.
Code Snippets:
//Function for getting a dec number from two hashes
function test_Rand(SERVER_SECRET, CLIENT_SECRET){
var client_hash = require('crypto'),
text = CLIENT_SECRET,
key = SERVER_SECRET
// create hash
var hash = client_hash.createHmac('sha512', key);
hash.update(text);
var clientRollAsHex = hash.digest('hex')
var serverRollAsHex = SERVER_SECRET;
//Add the numbers in Hex
var roll_hex = addHex(clientRollAsHex, serverRollAsHex);
//Cut the hex String
roll_hex = roll_hex.substring(0,4);
// Make INT
var roll_dec = parseInt(roll_hex, 16);
return roll_dec;
}
//Function for Hex-adding
function addHex(c1, c2) {
var hexStr = (parseInt(c1, 16) + parseInt(c2, 16)).toString(16);
while (hexStr.length < 6) { hexStr = '0' + hexStr; } // Zero pad.
return hexStr;
}
The SERVER_SECRET comes from the following func.
var secret = function() {
var r1 = Math.random()*10000000 + Math.floor(Date.now() / 1000);
var r2 = Math.random()*1000000 + Math.floor(Date.now() / 2000); //That does not make much sense
var new_hash = require('crypto');
var text = r1;
var key = r2;
// create hahs
var r_hash = new_hash.createHmac('sha512', key.toString());
r_hash.update(text.toString());
var retrn = r_hash.digest('hex');
return retrn;
}
It's because you are taking the first four digits that you get a skewed result.
For 50% of the numbers you will get a result that is 65 digits long instead of 64, and the first digit is 1. For example adding the two numbers in your example:
3CDC3C8C9...
CF27CC73E...
= 10C0409007...
Taking the first four digits from the result gives you 10C0. As you see from your result, there are a lot of numbers between 1000 (4096) and 1FFF (8191). Most of those are numbers where the result is 65 digits instead of 64.
If you instead take any four digits at a specific position (counter from the right), for example the last four digits, you will get a pretty even distribution of numbers between 0000 and FFFF.
A much simplified version of your design:
var a=Math.floor((Math.random() * 10));
var b=Math.floor((Math.random() * 10));
What is the range of a+b? 0-18
Now take only the first digit of the result. Then 0-9 would still be their original value *but 10-18 becomes 1.
To get the result you want, you need to remove the first digit for cases 10-18.

Node.js is trolling me. Generating random names and tweets using arrays

This question involves a snippet of code which is supposed to generate random tweets. However, I don't understand line by line what is happening; especially with the
Math.floor(Math.random() * arr.length)
My guess is that it picks a random array length, which is truncated to the lowest integer and then assigned as the array length of randArrayEl[].
However, I don't understand how it selects random first and last names with the following:
return randArrayEl(fakeFirsts) + " " + randArrayEl(fakeLasts);
Here's the whole code. Can anyone explain the logic of each line?
var randArrayEl = function(arr)
{
return arr[Math.floor(Math.random() * arr.length)];
};
var getFakeName = function()
{
var fakeFirsts = ['Nimit', 'Dave', 'Will', 'Charlotte', 'Jacob','Ethan','Sophia','Emma','Madison'];
var fakeLasts = ["Alley", 'Stacky', 'Fullstackerson', 'Nerd', 'Ashby', 'Gatsby', 'Hazelnut', 'Cookie', 'Tilde', 'Dash'];
return randArrayEl(fakeFirsts) + " " + randArrayEl(fakeLasts);
};
var getFakeTweet = function()
{
var awesome_adj = ['awesome','breathtaking','amazing','sexy','sweet','cool','wonderful','mindblowing'];
return "Fullstack Academy is " + randArrayEl(awesome_adj) + "! The instructors are just so " + randArrayEl(awesome_adj) + ". #fullstacklove #codedreams";
};
for(var i=0; i<10; i++)
{
store.push(getFakeName(), getFakeTweet());
}
Also, what is the for loop at the end supposed to do?
randArrayEl returns a random element from the array. It does this by picking a random integer between 0 and arr.length-1 (because Math.random() never returns exactly 1), and returning that element from the array.
So randArrayEl(fakeFirsts) returns a random name from the fakeFirsts array, and likewise for randArrayEl(fakeLasts). Concatenate them with a space and you have a random first and last name combo!
getFakeTweet works in a similar way with randArrayEl(awesome_adj) to describe Fullstack Academy.
Finally, the for loop puts 10 random tweets and associated random names in the store array.
randArrayEl picks a single random item from the array.
Take a look at the line from inside towards outside:
Math.random() gives you a random number between 0 and 1;
Math.random() * arr.length gives you a random number from 0 to n,
where n is the number of items in that array; but it's not a whole
number/integer;
Math.floor(Math.random() * arr.length) gives you a rounded integer
number from 0 to n;
arr[Math.floor(Math.random() * arr.length)] gives you the text that
corresponds to that number.
The for loop at the end puts a fake name and a fake tweet into the "store" and repeats it 10 times.
In the line
Math.floor(Math.random() * arr.length)
Math.random() returns a floating point number x in the range 0 <= x < 1. Multiplying that by arr.length gives you a floating point number in the range 0 <= x < arr.length. Taking math.Floor of that removes the fractional part, so you are left with an integer n in the range 0 <= n <= arr.length-1.
If you now access array[n], for some random n, you will get a random string from the array of strings. So the line
return randArrayEl(fakeFirsts) + " " + randArrayEl(fakeLasts);
returns a random first name from fakeFirsts concatenated with a random second name chosen from fakeLasts (separated by a space).

assigning x amount of random numbers to an array

I currently have 64 values (1-64) and each value returns a different outcome.
I have an array setup with a range of numbers
var battleships = ['1', '2', '3', '6']
which links with
if ($.inArray(String($(this).data("value")), battleships) > -1) {
$(this).addClass("success").removeClass("pointer");
}
how would i make the array assign (for example) 26 random numbers between 1 and 64 which could then be picked up by the if statement.
You can have a function to get random numbers between min and max like this:
function getRandomBetween(min, max) {
return Math.random() * (max - min) + min;
}
And then you can loop 64 times to fill your array.
for (var i = 0; i < 64; i++) {
battleships.push(getRandomBetween(1, 64));
}
function getDistinctNumbers(count) {
var values = [];
for (var i=1; i<=64; i++)
values.push(i);
values.sort(function(a,b){return Math.round(Math.random() * 2) -1;});
values.length = count +1;
return values;
}
Basically you fill an array with the values you want, shuffle it around and then reduce its length to fit your needs. If count is larger than 64, the array will be padded with undefined values.
If your needs change and for some reason, the 64 gets turned into a large number, while the 24 remains relatively low, it will be faster to simply add random numbers and make sure you don't add duplicates than to sort randomly a huge array, but for now this is the fastest solution.
Here is a JsBin with the code: http://jsbin.com/UwAYACe/1/edit

Generate 1000 random 10 digit number using javascript in a loop

I want to generate atleast 1000 unique random numbers with 10 digit using
javascript in a loop. Is this possible? Or is Javascript a wrong thing to do this?
UPDATE: How will you be sure that duplicates are not created?
Here's how I would do it:
var arr = [],
track = [],
min = 1000000000,
max = 9999999999,
qty = 1000,
ii = 0,
rnd;
while (ii < qty) {
rnd = Math.floor(Math.random() * (max - min + 1)) + min;
if (!track[rnd]) {
arr[ii] = track[rnd] = rnd;
ii += 1;
}
}
Here's a working example: http://jsfiddle.net/mTmEs/
Now, if something went awry with Math.random and for some reason it were to generate a lot of duplicates, this code could take a long time to complete. I don't think there is any way around this kind of potential problem though when you're talking about large quantities of unique random numbers.
Yes, it's possible.
Use Math.random to generate the pseudo-random numbers. Math.random returns a pseudo-random number greater than or equal to 0 and less than 1, so to get a 10-digit number (I'm assuming a whole number), you'd multiple that by 1,000,000,000 and round it off with Math.round or Math.floor. (If you need them all to be 10 digits, adjust accordingly — adding a base amount, multiplying by a higher number, etc.)
Use an object to keep track of them, so var obj = {}; to start with.
Store the numbers as keys in an object, e.g. obj[number] = true.
Test whether the object has the generated number using if (obj[number])
Loop until you have the correct number of unique numbers.
The reason I use an object for storing the numbers is that JavaScript objects are maps by their nature, and engines are optimized to retrieve properties from objects quickly. Under the covers, an implementation can do what it likes, but will probably use a hash table or similar.
Note that using an "array" for this is unnecessary; JavaScript arrays aren't really arrays, they're just objects with a couple of special features.
You could do this in JS, but you would have to check the array to see if it contains the currently generated random number. This would obviously degrade performance.
See if these answers help.
Random number generator that fills an interval
Generate unique random numbers between 1 and 100
A generic function for generating n random numbers of length l might be:
// Generate n unique random numbers of length l
// l should be less than 15
function genNRandLen(n, l) {
// Make sure l and n are numbers
n = Number(n);
l = Number(l);
// Protect against bad input
if (isNaN(l) || isNaN(n)) return;
var o = {}, a = [], num;
var min = l == 1? 0 : Math.pow(10, l-1);
var r = Math.pow(10, l) - min;
// Protect against endless loop
if (n >= (r)) return;
while (n--) {
do {
num = Math.floor(min + (Math.random()*r));
} while (o[num])
o[num] = true;
a[n] = num;
}
return a.sort();
}
Sorting is just to make it easy to see duplicates when testing, remove if not necessary or random order is preferred.
If numbers longer than 15 digits are required, they can be created by concatenating strings of shorter random numbers and trimming to the required length. The following will generate a random number of any length:
// Generate random number of length l
function randLen(l) {
var n = '';
while (n.length < l) {
n += String(Math.random()).replace(/^0\.0*/,'');
}
return n.substring(0, l);
}
It must return a string since converting to number will mess with the results. Oh, and all numbers are integers.
Why not? Here is the code:
var a=[];
for (var i=1000; i--;)
a.push(Math.random()*10000000000)

Categories