function rand
rand(): number

Uniformly distributed pseudorandom numbers.

Generates uniformly distributed pseudorandom numbers. It can return:

  • A single random number if no arguments are provided.
  • A 2D array of random numbers if dimensions are provided.

Examples

Single random number

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(typeof rand(), 'number');

Empty matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(rand(0), []);

1x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x1 = rand(1) as matrix;
assertEquals(result1x1.length, 1);
assertEquals(result1x1[0].length, 1);

2x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x2 = rand(2) as matrix;
assertEquals(result2x2.length, 2);
assertEquals(result2x2[0].length, 2);

2x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x1 = rand([2, 1]) as matrix;
assertEquals(result2x1.length, 2);
assertEquals(result2x1[0].length, 1);

1x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x2 = rand(1, 2) as matrix;
assertEquals(result1x2.length, 1);
assertEquals(result1x2[0].length, 2);

Return Type

number

A random number or a matrix of random numbers

Throws

If invalid arguments are provided

rand(n: number): matrix

Uniformly distributed pseudorandom numbers.

Generates uniformly distributed pseudorandom numbers. It can return:

  • A single random number if no arguments are provided.
  • A 2D array of random numbers if dimensions are provided.

Examples

Single random number

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(typeof rand(), 'number');

Empty matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(rand(0), []);

1x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x1 = rand(1) as matrix;
assertEquals(result1x1.length, 1);
assertEquals(result1x1[0].length, 1);

2x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x2 = rand(2) as matrix;
assertEquals(result2x2.length, 2);
assertEquals(result2x2[0].length, 2);

2x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x1 = rand([2, 1]) as matrix;
assertEquals(result2x1.length, 2);
assertEquals(result2x1[0].length, 1);

1x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x2 = rand(1, 2) as matrix;
assertEquals(result1x2.length, 1);
assertEquals(result1x2[0].length, 2);

Parameters

n: number

Return Type

A random number or a matrix of random numbers

Throws

If invalid arguments are provided

rand(dims: array): matrix

Uniformly distributed pseudorandom numbers.

Generates uniformly distributed pseudorandom numbers. It can return:

  • A single random number if no arguments are provided.
  • A 2D array of random numbers if dimensions are provided.

Examples

Single random number

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(typeof rand(), 'number');

Empty matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(rand(0), []);

1x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x1 = rand(1) as matrix;
assertEquals(result1x1.length, 1);
assertEquals(result1x1[0].length, 1);

2x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x2 = rand(2) as matrix;
assertEquals(result2x2.length, 2);
assertEquals(result2x2[0].length, 2);

2x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x1 = rand([2, 1]) as matrix;
assertEquals(result2x1.length, 2);
assertEquals(result2x1[0].length, 1);

1x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x2 = rand(1, 2) as matrix;
assertEquals(result1x2.length, 1);
assertEquals(result1x2[0].length, 2);

Parameters

dims: array

Return Type

A random number or a matrix of random numbers

Throws

If invalid arguments are provided

rand(
rows: number,
cols: number,
): matrix

Uniformly distributed pseudorandom numbers.

Generates uniformly distributed pseudorandom numbers. It can return:

  • A single random number if no arguments are provided.
  • A 2D array of random numbers if dimensions are provided.

Examples

Single random number

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(typeof rand(), 'number');

Empty matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

assertEquals(rand(0), []);

1x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x1 = rand(1) as matrix;
assertEquals(result1x1.length, 1);
assertEquals(result1x1[0].length, 1);

2x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x2 = rand(2) as matrix;
assertEquals(result2x2.length, 2);
assertEquals(result2x2[0].length, 2);

2x1 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result2x1 = rand([2, 1]) as matrix;
assertEquals(result2x1.length, 2);
assertEquals(result2x1[0].length, 1);

1x2 matrix

import { assertEquals } from "jsr:@std/assert";
import type { matrix } from "../types.d.ts";

const result1x2 = rand(1, 2) as matrix;
assertEquals(result1x2.length, 1);
assertEquals(result1x2[0].length, 2);

Parameters

rows: number
cols: number

Return Type

A random number or a matrix of random numbers

Throws

If invalid arguments are provided