Untitled
Generating random numbers using Pi and Math.Random()unknown
plain_text
2 years ago
4.8 kB
12
Indexable
//javascript code:
const PI = require('pi');
const piStr = PI(1000000, false);
console.log('---- GENERATING RANDOM NUMBERS USING PI ----');
console.log(`Pi string length: ${piStr.length}\t(Should be equal to 1 million -> ${piStr.length === 1000000})`);
let counts = {};
function getRandomPiDigit(iterations, label) {
for (let i = 0; i < iterations; i++) {
const randomIndex = Math.floor(Math.random() * piStr.length);
const randomPiDigit = piStr.charAt(randomIndex);
counts[randomPiDigit] = (counts[randomPiDigit] ?? 0) + 1;
}
let sum = 0n;
let count = 0n;
for (const key in counts) {
sum += BigInt(counts[key]);
count++;
}
const avg = sum / count;
console.log(`${label} loop - average: ${avg}`, counts);
counts = {};
}
function getRandomDigit(iterations, label) {
for (let i = 0; i < iterations; i++) {
const randomDigit = Math.floor(Math.random() * 10);
counts[randomDigit] = (counts[randomDigit] ?? 0) + 1;
}
let sum = 0n;
let count = 0n;
for (const key in counts) {
sum += BigInt(counts[key]);
count++;
}
const avg = sum / count;
console.log(`${label} loop - average: ${avg}`, counts);
counts = {};
}
let iterations = 1000;
getRandomPiDigit(iterations, '1 thousand');
iterations = iterations * 10;
getRandomPiDigit(iterations, '10 thousand');
iterations = iterations * 10;
getRandomPiDigit(iterations, '100 thousand');
iterations = iterations * 10;
getRandomPiDigit(iterations, '1 million');
iterations = iterations * 10;
getRandomPiDigit(iterations, '10 million');
iterations = iterations * 10;
getRandomPiDigit(iterations, '100 million');
console.log('---- GENERATING RANDOM NUMBERS USING Math.random() ----');
iterations = 1000;
getRandomDigit(iterations, '1 thousand');
iterations = iterations * 10;
getRandomDigit(iterations, '10 thousand')
iterations = iterations * 10;
getRandomDigit(iterations, '100 thousand')
iterations = iterations * 10;
getRandomDigit(iterations, '1 million')
iterations = iterations * 10;
getRandomDigit(iterations, '10 million')
iterations = iterations * 10;
getRandomDigit(iterations, '100 million');
console.log('\t/Fin');
//console output:
---- GENERATING RANDOM NUMBERS USING PI ----
Pi string length: 1000000 (Should be equal to 1 million -> true)
1 thousand loop - average: 100 {
'0': 100,
'1': 101,
'2': 98,
'3': 98,
'4': 105,
'5': 85,
'6': 94,
'7': 109,
'8': 109,
'9': 101
}
10 thousand loop - average: 1000 {
'0': 1001,
'1': 1045,
'2': 1006,
'3': 980,
'4': 981,
'5': 1019,
'6': 1026,
'7': 948,
'8': 1014,
'9': 980
}
100 thousand loop - average: 10000 {
'0': 9999,
'1': 9986,
'2': 9855,
'3': 10194,
'4': 10216,
'5': 10076,
'6': 9809,
'7': 9773,
'8': 10079,
'9': 10013
}
1 million loop - average: 100000 {
'0': 99525,
'1': 99723,
'2': 100304,
'3': 100454,
'4': 100391,
'5': 100103,
'6': 99491,
'7': 99819,
'8': 99926,
'9': 100264
}
10 million loop - average: 1000000 {
'0': 1000287,
'1': 998665,
'2': 1000427,
'3': 1003428,
'4': 1002388,
'5': 1002569,
'6': 993720,
'7': 997913,
'8': 1000152,
'9': 1000451
}
100 million loop - average: 10000000 {
'0': 9992397,
'1': 9970922,
'2': 10004350,
'3': 10019078,
'4': 10021982,
'5': 10036580,
'6': 9950430,
'7': 9981039,
'8': 10003997,
'9': 10019225
}
---- GENERATING RANDOM NUMBERS USING Math.random() ----
1 thousand loop - average: 100 {
'0': 101,
'1': 109,
'2': 101,
'3': 100,
'4': 91,
'5': 107,
'6': 94,
'7': 104,
'8': 94,
'9': 99
}
10 thousand loop - average: 1000 {
'0': 975,
'1': 961,
'2': 1023,
'3': 1040,
'4': 1018,
'5': 1005,
'6': 1012,
'7': 1025,
'8': 932,
'9': 1009
}
100 thousand loop - average: 10000 {
'0': 9944,
'1': 10024,
'2': 10319,
'3': 9909,
'4': 9945,
'5': 10051,
'6': 9944,
'7': 9991,
'8': 9996,
'9': 9877
}
1 million loop - average: 100000 {
'0': 99589,
'1': 100013,
'2': 100304,
'3': 99716,
'4': 99715,
'5': 100284,
'6': 99878,
'7': 100116,
'8': 100289,
'9': 100096
}
10 million loop - average: 1000000 {
'0': 998457,
'1': 998674,
'2': 998140,
'3': 999321,
'4': 1000176,
'5': 1001394,
'6': 1000630,
'7': 1001197,
'8': 1000380,
'9': 1001631
}
100 million loop - average: 10000000 {
'0': 9998174,
'1': 10005137,
'2': 9998211,
'3': 9996653,
'4': 10000285,
'5': 10004757,
'6': 9998632,
'7': 9999659,
'8': 10001125,
'9': 9997367
}
/Fin
Editor is loading...
Leave a Comment