Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10891x 10891x 10891x 10891x 10891x 10891x 10891x 10891x 10891x 10891x 10891x 10891x 10891x 16x 10891x 15x 15x 15x 5x 5x 3x 3x 3x 5x 15x 13x 13x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 12x 13x 15x 10880x 10891x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isObject = require( '@stdlib/assert/is-plain-object' );
var isArray = require( '@stdlib/assert/is-array' );
var contains = require( '@stdlib/array/base/assert/contains' ).factory;
var Int32Array = require( '@stdlib/array/int32' );
var Uint32Array = require( '@stdlib/array/uint32' );
var table = require( './prngs.js' );
// VARIABLES //
var TYPED_ARRAY_CTORS = {
'Int32Array': Int32Array,
'Uint32Array': Uint32Array
};
var PRNG_WRAPPERS = [ 'randi', 'randn', 'randu' ];
var isPRNGWrapper = contains( PRNG_WRAPPERS );
// MAIN //
/**
* Revives a JSON-serialized pseudorandom number generator.
*
* @param {string} key - key
* @param {*} value - value
* @returns {(*|Function)} value or PRNG
*
* @example
* var parseJSON = require( '@stdlib/utils/parse-json' );
* var mt19937 = require( '@stdlib/random/base/mt19937' );
*
* var str = JSON.stringify( mt19937 );
* var rand = parseJSON( str, reviveBasePRNG );
* // returns <Function>
*/
function reviveBasePRNG( key, value ) {
var factory;
var opts;
var args;
var ctor;
var tmp;
if (
value &&
value.type === 'PRNG' &&
isString( value.name ) &&
isObject( value.state ) &&
isArray( value.params ) &&
isString( value.state.type ) &&
isArray( value.state.data )
) {
opts = {};
factory = table[ value.name ];
if ( factory === void 0 ) {
tmp = value.name.split( '-' );
if ( isPRNGWrapper( tmp[ 0 ] ) ) {
factory = table[ tmp[ 0 ] ];
opts.name = tmp.slice( 1 ).join( '-' );
}
}
if ( factory ) {
ctor = TYPED_ARRAY_CTORS[ value.state.type ];
if ( ctor ) {
opts.state = new ctor( value.state.data );
args = value.params.slice();
args.push( opts );
try {
return factory.apply( null, args );
} catch ( error ) { // eslint-disable-line no-unused-vars
// Return the original JSON value...
}
}
}
}
return value;
}
// EXPORTS //
module.exports = reviveBasePRNG;
|