rand

Syntax

rand(X, count)

Arguments

X is a scalar/vector.

count is an INT scalar/pair. As a scalar, it indicates the length of the output vector; As a pair, it indicates the dimension of the output matrix.

Details

Return a vector/matrix of random values.

  • If X is a scalar, return a vector of random values that follows a uniform distribution on [0, X).

  • If X is a vector, return a vector of random values drawn from the elements of X.

The data type of the result is the same as the data type of X.

Examples

$ x=rand(10, 20);

// generate 20 random nonnegative integers less than 10
$ x;
[9,9,8,1,1,0,8,3,2,6,4,6,9,6,8,9,3,2,1,5]

// generate 10 random nonnegative double values less than 9.8
$ rand(9.8, 10);
[3.653754,1.750518,0.055747,5.219222,2.473778,6.337576,7.797493,1.392241,0.149499,5.697612]

// generate 3 random values drawn from vector x
$ x=3 5 4 6 9;
$ rand(x, 3);
[9,3,6]

$ rand(10.0, 2:2) // generate a 2*2 random matrix less than 10

col1

col2

0.8233

1.0052

7.1127

9.7578

$ 12:35:06 + rand(100, 10);
[12:35:44,12:35:16,12:35:50,12:35:44,12:35:46,12:35:09,12:35:50,12:36:35,12:35:09,12:36:44]

$ x=`IBM`C`AAPL`BABA;
$ rand(x, 10);
["IBM","BABA","C","AAPL","IBM","C","BABA","AAPL","BABA","BABA"]