To generate a random number between a range x
and y
in MATLAB, you can use the rand
function to generate a random number from a uniform distribution and then scale it to the desired range using some basic algebra:
main.m130 chars4 lines
In this code, rand
generates a random number between 0 and 1 from a uniform distribution. The expression (y-x)*rand
scales the random number to be within the desired range, and the x +
part shifts it to the right place.
You can easily generate multiple random numbers at once by specifying the size of the output array as an argument to rand
, and then scaling and shifting the result array as needed:
main.m186 chars5 lines
gistlibby LogSnag