PageSourceSearch

https://cdn4.hola.org/util/rand.js

js hola.org collected 2026-10-01 13:03:42 UTC 2,003 bytes, 86 lines download raw bytes

1// LICENSE_CODE ZON ISC
2'use strict'; /*zlint node, br*/
3(function(){
4var define;
5var is_node = typeof module=='object' && module.exports && module.children;
6if (!is_node)
7    define = self.define;
8else
9    define = require('./require_node.js').define(module, '../');
10define(['/util/array.js'], function(array){
11var E = {};
12
13var is_jtest = false;
14var jtest_vals = {};
15var MAX_INT = 2147483647;
16var MIN_INT = -MAX_INT-1;
17
18function jtest_pop(s){
19    var elm;
20    if (s===undefined)
21        return null;
22    if ((elm = jtest_vals[s])===undefined)
23        return null;
24    return elm.shift();
25}
26
27E.rand = function(s){
28    var ret;
29    if (is_jtest && (ret = jtest_pop(s))!==null)
30        return ret;
31    return Math.random();
32};
33
34E.rand_int32 = function(s){
35    var ret;
36    if (is_jtest && (ret = jtest_pop(s))!==null)
37        return ret;
38    return Math.floor(Math.random()*(MAX_INT-MIN_INT+1))-MAX_INT;
39};
40
41E.rand_range = function(from, to, s){
42    var ret;
43    if (is_jtest && (ret = jtest_pop(s))!==null)
44        return ret;
45    return Math.floor(Math.random()*(to-from))+from;
46};
47
48E.rand_element = function(a, s){
49    if (a.length)
50        return a[E.rand_range(0, a.length, s)];
51};
52
53E.rand_subset = function(a, size, s){
54    // Fisher-Yates-Knuth shuffle
55    var shuffled = a.slice(0);
56    for (var i=0; i<size; i++)
57    {
58        var j = E.rand_range(i, shuffled.length, s);
59        var tmp = shuffled[j];
60        shuffled[j] = shuffled[i];
61        shuffled[i] = tmp;
62    }
63    return shuffled.slice(0, size);
64};
65
66E.jtest_push = function(s, arr){
67    if (!jtest_vals[s])
68        jtest_vals[s] = [];
69    if (Array.isArray(arr))
70        array.push(jtest_vals[s], arr);
71    else
72        jtest_vals[s].push(arr);
73};
74
75E.jtest_init = function(){
76    is_jtest = true;
77    jtest_vals = {};
78};
79
80E.jtest_uninit = function(){
81    is_jtest = false; };
82
83E.basic_u32 = function(v){ return (1103515245*v+12345) >>> 0; };
84E.basic_u31 = function(v){ return (1103515245*v+12345) & 0x7fffffff; };
85
86return E; }); }());

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.