Skip to content

Commit

Permalink
Doc:improve README examples of random/strided/tools namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
kohantikanath committed Jul 28, 2024
1 parent bd64130 commit 108a839
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions lib/node_modules/@stdlib/random/strided/tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,52 @@ The namespace contains the following:
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/random/strided/tools' );

console.log( objectKeys( ns ) );
var tools = require( '@stdlib/random/strided/tools' );
var uniform = require( '@stdlib/random/base/uniform' );
var normal = require( '@stdlib/random/base/normal' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var filled = require( '@stdlib/array/filled' );
var zeros = require( '@stdlib/array/zeros' );
var array = require( '@stdlib/ndarray/array' );

// Create PRNG factories
var uniformFactory = tools.unaryFactory( uniform );
var normalFactory = tools.binaryFactory( normal );
var bernoulliFactory = tools.unaryFactory( bernoulli );

// Create uniform PRNG for filling strided arrays
var uniformFill = uniformFactory( -5.0, 5.0 );

// Create normal PRNG for filling strided arrays
var normalFill = normalFactory( 0.0, 1.0 );

// Create Bernoulli PRNG for filling strided arrays
var bernoulliFill = bernoulliFactory( 0.5 );

// Create arrays to fill
var x = zeros( 10 );
var y = filled( null, 10 );
var z = array( new Float64Array( 10 ), {
'dtype': 'float64'
});

// Fill arrays with random values
uniformFill( x.length, x, 1 );
normalFill( y.length, y, 1 );
bernoulliFill( z.data.length, z.data, 1 );

console.log( 'Uniform (-5, 5):' );
console.log( x );
console.log( '\nNormal (0, 1):' );
console.log( y );
console.log( '\nBernoulli (p=0.5):' );
console.log( z.data );

// Demonstrate stride manipulation
var w = zeros( 20 );
uniformFill( 5, w, 4 );
console.log( '\nUniform with stride 4:' );
console.log( w );
```

</section>
Expand Down

0 comments on commit 108a839

Please sign in to comment.