help rand rand Uniformly distributed pseudorandom numbers. R = rand(N) returns an N-by-N matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval(0,1). rand(M,N) or rand([M,N]) returns an M-by-N matrix. rand(M,N,P,...) or rand([M,N,P,...]) returns an M-by-N-by-P-by-... array. rand returns a scalar. rand(SIZE(A)) returns an array the same size as A. Note: The size inputs M, N, P, ... should be nonnegative integers. Negative integers are treated as 0. R = rand(..., CLASSNAME) returns an array of uniform values of the specified class. CLASSNAME can be 'double' or 'single'. R = rand(..., 'like', Y) returns an array of uniform values of the same class as Y. The sequence of numbers produced by rand is determined by the settings of the uniform random number generator that underlies rand, RANDI, and RANDN. Control that shared random number generator using RNG. Examples: Example 1: Generate values from the uniform distribution on the interval [a, b]. r = a + (b-a).*rand(100,1); Example 2: Use the RANDI function, instead of rand, to generate integer values from the uniform distribution on the set 1:100. r = randi(100,1,5); Example 3: Reset the random number generator used by rand, RANDI, and RANDN to its default startup settings, so that rand produces the same random numbers as if you restarted MATLAB. rng('default') rand(1,5) Example 4: Save the settings for the random number generator used by rand, RANDI, and RANDN, generate 5 values from rand, restore the settings, and repeat those values. s = rng u1 = rand(1,5) rng(s); u2 = rand(1,5) % contains exactly the same values as u1 Example 5: Reinitialize the random number generator used by rand, RANDI, and RANDN with a seed based on the current time. rand will return different values each time you do this. NOTE: It is usually not necessary to do this more than once per MATLAB session. rng('shuffle'); rand(1,5) See Updating Your Random Number Generator Syntax to use RNG to replace rand with the 'seed', 'state', or 'twister' inputs. See also randi, randn, rng, RandStream, RandStream/rand, sprand, sprandn, randperm. Other functions named rand Reference page in Help browser doc rand help factorial factorial Factorial function. factorial(N) for scalar N, is the product of all the integers from 1 to N, i.e. prod(1:N). When N is an N-D matrix, factorial(N) is the factorial for each element of N. Since double precision numbers only have about 15 digits, the answer is only accurate for N <= 21. For larger N, the answer will have the correct order of magnitude, and is accurate for the first 15 digits. Class support for input N: float: double, single integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 See also prod. Other functions named factorial Reference page in Help browser doc factorial factorial(12) ans = 479001600 help nchoosek nchoosek Binomial coefficient or all combinations. nchoosek(N,K) where N and K are non-negative integers returns N!/K!(N-K)!. This is the number of combinations of N things taken K at a time. When a coefficient is large, a warning will be produced indicating possible inexact results. In such cases, the result is only accurate to 15 digits for double-precision inputs, or 8 digits for single-precision inputs. nchoosek(V,K) where V is a vector of length N, produces a matrix with N!/K!(N-K)! rows and K columns. Each row of the result has K of the elements in the vector V. This syntax is only practical for situations where N is less than about 15. Class support for inputs N,K: float: double, single integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 Class support for inputs V: float: double, single integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 logical, char See also perms. Other functions named nchoosek Reference page in Help browser doc nchoosek help perms perms All possible permutations. perms(1:N), or perms(V) where V is a vector of length N, creates a matrix with N! rows and N columns containing all possible permutations of the N elements. This function is only practical for situations where N is less than about 10 (for N=11, the output takes over 3 gigabytes). Class support for input V: float: double, single integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 logical, char See also nchoosek, randperm, permute. Reference page in Help browser doc perms perms(1:3) ans = 3 2 1 3 1 2 2 3 1 2 1 3 1 2 3 1 3 2 factorial(12)/(factorial(3)*factorial(2)*factorial(7)) ans = 7920 diary off