Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaner random numbers #18

Open
wants to merge 4 commits into
base: 2.0.0-cryptographically-secure-version
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions code/plain-javascript/2.0.0/rando-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

238 changes: 145 additions & 93 deletions code/plain-javascript/2.0.0/rando.js
Original file line number Diff line number Diff line change
@@ -1,152 +1,204 @@
function rando(arg1, arg2, arg3){
var isUndefined = (variable) => typeof variable === "undefined",
isNumber = (num) => typeof num === "number" && !isNaN(num),
isString = (str) => typeof str === "string",
isObject = (obj) => typeof obj === "object",
isArray = (arr) => !isUndefined(arr) && arr !== null && arr.constructor === Array,
cryptoRandom = () => {
try{
var cryptoRandoms, cryptoRandomSlices = [], cryptoRandom;
while((cryptoRandom = "." + cryptoRandomSlices.join("")).length < 30){
cryptoRandoms = (window.crypto || window.msCrypto).getRandomValues(new Uint32Array(5));
for(var i = 0; i < cryptoRandoms.length; i++){
var cryptoRandomSlice = cryptoRandoms[i] < 4000000000 ? cryptoRandoms[i].toString().slice(1) : "";
if(cryptoRandomSlice.length > 0) cryptoRandomSlices[cryptoRandomSlices.length] = cryptoRandomSlice;
}
function rando(arg1, arg2, arg3) {
const isUndefined = (variable) => typeof variable === "undefined",
isNumber = (num) => typeof num === "number" && !isNaN(num),
isString = (str) => typeof str === "string",
isObject = (obj) => typeof obj === "object",
isArray = (arr) =>
!isUndefined(arr) && arr !== null && arr.constructor === Array,
setExponent = (n) => ((n & 0x1fffff) | 0x3ff00000) >>> 0;

const BIG_ENDIAN =
new Uint16Array(new Uint8Array([0x45, 0xfe]).buffer)[0] === 0x45fe;
const cryptoRandom = () => {
try {
const random = new Uint32Array(2);
(window.crypto || window.msCrypto).getRandomValues(random);
const upperHalf = setExponent(random[0]);
const lowerHalf = random[1];
if (BIG_ENDIAN) {
return (
new Float64Array(
new Uint32Array([upperHalf, lowerHalf]).buffer
)[0] - 1
);
} else {
return (
new Float64Array(
new Uint32Array([lowerHalf, upperHalf]).buffer
)[0] - 1
);
}
return Number(cryptoRandom);
}
catch(e){
} catch (e) {
return Math.random();
}
};
try{
if(arg1 !== null && arg2 !== null && arg3 !== null){
if(isUndefined(arg1)){

try {
if (arg1 !== null && arg2 !== null && arg3 !== null) {
if (isUndefined(arg1)) {
//regular decimal
return cryptoRandom();
}

if(!!window.jQuery && arg1 instanceof jQuery && isUndefined(arg2)){

if (
!!window.jQuery &&
arg1 instanceof jQuery &&
isUndefined(arg2)
) {
//jQuery object
if(arg1.length == 0) return false;
if (arg1.length == 0) return false;
var index = rando(0, arg1.length - 1);
return {index:index, value:arg1.eq(index)};
return { index: index, value: arg1.eq(index) };
}

if(isNumber(arg1) && isNumber(arg2) && isString(arg3) && arg3.toLowerCase().trim() == "float"){

if (
isNumber(arg1) &&
isNumber(arg2) &&
isString(arg3) &&
arg3.toLowerCase().trim() == "float"
) {
//float from min to max (inclusive of min and exclusive of max)
if(arg1 > arg2) var temp = arg2, arg2 = arg1, arg1 = temp;
if (arg1 > arg2)
var temp = arg2,
arg2 = arg1,
arg1 = temp;
return cryptoRandom() * (arg2 - arg1) + arg1;
}
if(isArray(arg1) && arg1.length > 0 && isUndefined(arg2)){

if (isArray(arg1) && arg1.length > 0 && isUndefined(arg2)) {
//array
var arr = arg1, pickedIndex = cryptoRandom() * arr.length << 0;
return {index: pickedIndex, value: arr[pickedIndex]};
var arr = arg1,
pickedIndex = (cryptoRandom() * arr.length) << 0;
return { index: pickedIndex, value: arr[pickedIndex] };
}
if(isObject(arg1) && isUndefined(arg2)){

if (isObject(arg1) && isUndefined(arg2)) {
//object
var obj = arg1, keys = Object.keys(obj);
if(keys.length > 0){
var key = keys[keys.length * cryptoRandom() << 0];
return {key: key, value: obj[key]};
var obj = arg1,
keys = Object.keys(obj);
if (keys.length > 0) {
var key = keys[(keys.length * cryptoRandom()) << 0];
return { key: key, value: obj[key] };
}
}

if(((arg1 === true && arg2 === false) || (arg1 === false && arg2 === true)) && isUndefined(arg3)){

if (
((arg1 === true && arg2 === false) ||
(arg1 === false && arg2 === true)) &&
isUndefined(arg3)
) {
//boolean
return rando() < .5;
return rando() < 0.5;
}
if(isNumber(arg1) && isUndefined(arg2)){

if (isNumber(arg1) && isUndefined(arg2)) {
//int from 0 through max OR min through 0 if negative (inclusive of both min and max)
if(arg1 >= 0) return rando(0, arg1);
if (arg1 >= 0) return rando(0, arg1);
return rando(arg1, 0);
}

if(isNumber(arg1) && isString(arg2) && arg2.toLowerCase().trim() == "float" && isUndefined(arg3)){

if (
isNumber(arg1) &&
isString(arg2) &&
arg2.toLowerCase().trim() == "float" &&
isUndefined(arg3)
) {
//float from 0 to max OR min to 0 if negative (inclusive of min and exclusive of max)
return arg1 >= 0 ? rando(0, arg1, "float") : rando(arg1, 0, "float");
return arg1 >= 0
? rando(0, arg1, "float")
: rando(arg1, 0, "float");
}
if(isNumber(arg1) && isNumber(arg2) && isUndefined(arg3)){

if (isNumber(arg1) && isNumber(arg2) && isUndefined(arg3)) {
//int from min through max (inclusive of both min and max)
if(arg1 > arg2) var temp = arg2, arg2 = arg1, arg1 = temp;
arg1 = Math.floor(arg1), arg2 = Math.floor(arg2);
if (arg1 > arg2)
var temp = arg2,
arg2 = arg1,
arg1 = temp;
(arg1 = Math.floor(arg1)), (arg2 = Math.floor(arg2));
return Math.floor(cryptoRandom() * (arg2 - arg1 + 1) + arg1);
}
if(isString(arg1) && arg1.length > 0 && isUndefined(arg2)){

if (isString(arg1) && arg1.length > 0 && isUndefined(arg2)) {
//string
return arg1.charAt(rando(0, arg1.length - 1));
}
}
return false;
}
catch(e){
} catch (e) {
return false;
}
}

function randoSequence(arg1, arg2){
function randoSequence(arg1, arg2) {
var isUndefined = (variable) => typeof variable === "undefined",
isNumber = (num) => typeof num === "number" && !isNaN(num),
isString = (str) => typeof str === "string",
isObject = (obj) => typeof obj === "object",
isArray = (arr) => !isUndefined(arr) && arr !== null && arr.constructor === Array;

try{
if(isUndefined(arg1) || arg1 === null || arg2 === null) return false;//invalid arguments

isNumber = (num) => typeof num === "number" && !isNaN(num),
isString = (str) => typeof str === "string",
isObject = (obj) => typeof obj === "object",
isArray = (arr) =>
!isUndefined(arr) && arr !== null && arr.constructor === Array;

try {
if (isUndefined(arg1) || arg1 === null || arg2 === null) return false; //invalid arguments

var arr = [];
if(!!window.jQuery && arg1 instanceof jQuery && isUndefined(arg2)){

if (!!window.jQuery && arg1 instanceof jQuery && isUndefined(arg2)) {
//jQuery object
if(arg1.length > 0){
if (arg1.length > 0) {
arr = randoSequence(0, arg1.length - 1);
for(var i = 0; i < arr.length; i++) arr[i] = {index:arr[i], value:arg1.eq(arr[i])};
for (var i = 0; i < arr.length; i++)
arr[i] = { index: arr[i], value: arg1.eq(arr[i]) };
}
return arr;
}

if(!isUndefined(arg2)){
if(!isNumber(arg1) || !isNumber(arg2) || arg1 % 1 > 0 || arg2 % 1 > 0) return false;//invalid arguments


if (!isUndefined(arg2)) {
if (
!isNumber(arg1) ||
!isNumber(arg2) ||
arg1 % 1 > 0 ||
arg2 % 1 > 0
)
return false; //invalid arguments

//int from min through max (inclusive of both min and max)
if(arg1 > arg2) var temp = arg2, arg2 = arg1, arg1 = temp;
for(var i = arg1; i <= arg2; i++) arr[arr.length] = i;
}
else if(isArray(arg1) && isUndefined(arg2)){
if (arg1 > arg2)
var temp = arg2,
arg2 = arg1,
arg1 = temp;
for (var i = arg1; i <= arg2; i++) arr[arr.length] = i;
} else if (isArray(arg1) && isUndefined(arg2)) {
//array
for(var i = 0; i < arg1.length; i++) arr[arr.length] = {index:i, value:arg1[i]};
}
else if(isObject(arg1) && isUndefined(arg2)){
for (var i = 0; i < arg1.length; i++)
arr[arr.length] = { index: i, value: arg1[i] };
} else if (isObject(arg1) && isUndefined(arg2)) {
//object
for(var prop in arg1) if(Object.prototype.hasOwnProperty.call(arg1, prop)) arr[arr.length] = {key:prop, value:arg1[prop]};
}
else if(isString(arg1) && isUndefined(arg2)){
for (var prop in arg1)
if (Object.prototype.hasOwnProperty.call(arg1, prop))
arr[arr.length] = { key: prop, value: arg1[prop] };
} else if (isString(arg1) && isUndefined(arg2)) {
//string
for(var i = 0; i < arg1.length; i++) arr[arr.length] = arg1.charAt(i);
}
else if(isNumber(arg1) && isUndefined(arg2)){
for (var i = 0; i < arg1.length; i++)
arr[arr.length] = arg1.charAt(i);
} else if (isNumber(arg1) && isUndefined(arg2)) {
//int from 0 through max OR min through 0 if negative (inclusive of both min and max)
return arg1 >= 0 ? randoSequence(0, arg1) : randoSequence(arg1, 0);
}
else{
} else {
//invalid arguments
return false;
}

//shuffle values
var indexToSwapWith;
for(i = arr.length - 1; i > 0; i--) indexToSwapWith = rando(i), temp = arr[i], arr[i] = arr[indexToSwapWith], arr[indexToSwapWith] = temp;

for (i = arr.length - 1; i > 0; i--)
(indexToSwapWith = rando(i)),
(temp = arr[i]),
(arr[i] = arr[indexToSwapWith]),
(arr[indexToSwapWith] = temp);

return arr;
}
catch(e){
} catch (e) {
return false;
}
}