[php] seedrandom.php
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- <?php
- $var = '* @param {number=} overflow
- * @param {number=} startdenom
- */
- //função anônima
- (function (pool, math, width, chunks, significance, overflow, startdenom) {
- //
- // seedrandom()
- // Esta é a função seedrandom descrita acima.
- //
- math[\'seedrandom\'] = function seedrandom(seed, use_entropy) {
- var key = [];
- var arc4;
- // Achatamos a string de semente ou construímos uma a partir da entropia local, se necessário.
- seed = mixkey(flatten(
- use_entropy ? [seed, pool] :
- arguments.length ? seed :
- [new Date().getTime(), pool, window], 3), key);
- // Usamos a semente para inicializar um gerador ARC4.
- arc4 = new ARC4(key);
- // Misturamos a aleatoriedade na entropia acumulada.
- mixkey(arc4.S, pool);
- // Substituímos Math.random
- // Esta função retorna um número aleatório em [0, 1) que contém
- // aleatoriedade em cada bit da mantissa do valor IEEE 754.
- math[\'random\'] = function random() { // Closure para retornar um número aleatório:
- var n = arc4.g(chunks); // Começamos com um numerador n < 2 ^ 48
- var d = startdenom; // e denominador d = 2 ^ 48.
- var x = 0; // e sem \'último byte extra\'.
- while (n < significance) { // Preenchemos todos os dígitos significativos por
- n = (n + x) * width; // deslocamento do numerador e
- d *= width; // denominador e gerando um
- x = arc4.g(1); // novo byte menos significativo.
- }
- while (n >= overflow) { // Para evitar arredondamento para cima, antes de adicionar
- n /= 2; // último byte, deslocamos tudo
- d /= 2; // para a direita usando matemática de inteiros até
- x >>>= 1; // termos exatamente os bits desejados.
- }
- return (n + x) / d; // Formamos o número dentro de [0, 1).
- };
- // Retornamos a semente que foi usada
- return seed;
- };
- //
- // ARC4
- //
- // Uma implementação ARC4. O construtor recebe uma chave na forma de
- // uma matriz de no máximo (largura) inteiros que devem ser 0 <= x < (largura).
- //
- // O método g(contagem) retorna um número pseudorrandômico que concatena
- // as próximas (contagem) saídas de ARC4. Seu valor de retorno é um número x
- // que está no intervalo 0 <= x < (largura ^ contagem).
- //
- /** @constructor */
- function ARC4(key) {
- var t, u, me = this, keylen = key.length;
- var i = 0, j = me.i = me.j = me.m = 0;
- me.S = [];
- me.c = [];
- // A chave vazia [] é tratada como [0].
- if (!keylen) { key = [keylen++]; }
- // Configuramos S usando o algoritmo padrão de programação de chaves.
- while (i < width) { me.S[i] = i++; }
- for (i = 0; i < width; i++) {
- t = me.S[i];
- j = lowbits(j + t + key[i % keylen]);
- u = me.S[j];
- me.S[i] = u;
- me.S[j] = t;
- }
- // O método "g" retorna as próximas (contagem) saídas como um número.
- me.g = function getnext(count) {
- var s = me.S;
- var i = lowbits(me.i + 1); var t = s[i];
- var j = lowbits(me.j + t); var u = s[j];
- s[i] = u;
- s[j] = t;
- var r = s[lowbits(t + u)];
- while (--count) {
- i = lowbits(i + 1); t = s[i];
- j = lowbits(j + t); u = s[j];
- s[i] = u;
- s[j] = t;
- r = r * width + s[lowbits(t + u)];
- }
- me.i = i;
- me.j = j;
- return r;
- };
- // Para imprevisibilidade robusta, descartamos um lote inicial de valores.
- // Veja http://www.rsa.com/rsalabs/node.asp?id=2009
- me.g(width);
- }
- //
- // flatten()
- // Converte uma árvore de objetos em matrizes aninhadas de strings.
- //
- /** @param {Object=} result
- * @param {string=} prop */
- function flatten(obj, depth, result, prop) {
- result = [];
- if (depth && typeof(obj) == \'object\') {
- for (prop in obj) {
- if (prop.indexOf(\'S\') < 5) { // Evitamos o bug do FF3 (local/sessionStorage)
- try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {}
- }
- }
- }
- return result.length ? result : \'\' + obj;
- }
- //
- // mixkey()
- // Mistura uma semente de string em uma chave que é uma matriz de inteiros, e
- // retorna uma semente de string encurtada que é equivalente à chave resultante.
- //
- /** @param {number=} smear
- * @param {number=} j */
- function mixkey(seed, key, smear, j) {
- seed += \'\'; // Garantimos que a semente seja uma string
- smear = 0;
- for (j = 0; j < seed.length; j++) {
- key[lowbits(j)] =
- lowbits((smear ^= key[lowbits(j)] * 19) + seed.charCodeAt(j));
- }
- seed = \'\';
- for (j in key) { seed += String.fromCharCode(key[j]); }
- console.log(seed);
- return seed;
- }
- //
- // lowbits()
- // Um rápido "n mod largura" para largura uma potência de 2.
- //
- function lowbits(n) { return n & (width - 1); }
- //
- // As seguintes constantes estão relacionadas aos limites IEEE 754.
- //
- startdenom = math.pow(width, chunks);
- significance = math.pow(2, significance);
- overflow = significance * 2;
- //
- // Quando seedrandom.js é carregado, misturamos imediatamente alguns bits
- // do RNG incorporado no pool de entropia. Porque não queremos interferir
- // com o estado determinístico do PRNG mais tarde,
- // seedrandom não chamará math.random por conta própria novamente após
- // a inicialização.
- //
- mixkey(math.random(), pool);
- // Fim do escopo anônimo e passagem de valores iniciais.
- })(
- [], // pool: o pool de entropia começa vazio
- Math, // math: pacote contendo random, pow e seedrandom
- 256, // width: cada saída RC4 é 0 <= x < 256
- 6, // chunks: pelo menos seis saídas RC4 para cada número duplo
- 52 // significance: há 52 dígitos significativos em um número duplo
- );';
Editor
You can edit this paste and save as new: