Created
June 22, 2016 01:17
-
-
Save pachanka/ac72db6cf8c0ce5dc312fccccf9d7a0a to your computer and use it in GitHub Desktop.
Revisions
-
pachanka created this gist
Jun 22, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,95 @@ /* I MADE THIS, I THINK ITS AWESOME */ /* Seed generation and shuffle */ var Seed_int = 0; var Seed_str = ''; var Seed = { /* Generate a random number, or perpetuate a pseudo-random seed if it is provided. */ random_int : function(seed_int){ var rand = 0; if(typeof seed_int == 'number'){ // A linear congruential generator var multiplier = 1103515245; var increment = 12345; var modulus = Math.pow(2, 31); // 2^31 rand = ((multiplier * seed_int + increment) % modulus ); //seed_int++; }else{ // Create seed if(typeof window.crypto !== 'undefined' && typeof window.crypto.getRandomValues === 'function'){ // Sweeet crypto API var rand_arr = new Uint32Array(1); window.crypto.getRandomValues(rand_arr); rand = rand_arr[0]; }else{ // Insecure! rand = Math.floor(Math.random() * 10000000000); } } return rand; }, /* Can generate an integer from a string. */ int_from_string : function(str){ var hash = 0; var len = str.length; if (len == 0) return hash; for (i = 0; i < len; i++) { var ch = str.charCodeAt(i); hash = ( (hash << 5) - hash) + ch; hash = hash & hash; // Convert to 32bit integer } return hash; }, /* - len is desired string length - corpus is the available characters to construct the output - seed is a number */ string_from_seed : function(len, corpus, seed){ var text = ''; // Make sure corpus is at len lenght. if(len > corpus.length){ for (var i = 0; i < len; i++) { corpus += corpus.charAt(i); }; } return Seed.shuffle_string(corpus, seed).substring(0, len); }, /* - corpus: the string to shuffle - seed: is an integer */ shuffle_string : function(corpus, seed) { var a = corpus.split(''), n = a.length; for(var i = n - 1; i > 0; i--) { var j = Seed.random_int(seed) % (i + 1); var tmp = a[i]; a[i] = a[j]; a[j] = tmp; } return a.join(''); }, /* reset seed globals */ reset_seed : function(){ Seed_int = 0; Seed_str = ''; }, set_seed : function(seed_obj){ Seed_str = seed_obj.str; Seed_int = seed_obj.int; return seed_obj; }, make_seed_str : function(len,corpus){ //Seed.reset_seed(); return Seed.string_from_seed(len, corpus, Seed.random_int()); }, };