How to generate a matrix of random decimal values from -1 to 1?
9 views (last 30 days)
Show older comments
I want to generate a 8x10 matrix full of random decimal values from -1 to 1, each with EXACTLY 6 decimal places (i.e 0.523483, -0.932401, etc.).
I know that this code generates values with 7 decimal places (not sure how to get it to 6). But, I don't know how to automatically generate a matrix full of values like these (without using a for loop). Any ideas? thanks!
format long
disp(single(rand(3)))
1 Comment
Rik
on 27 Jul 2021
Note that single doesn't have normal decimal places, as it is a floating point number. They take this shape:
(-1)^sign*2^(exponent-127)x1.fraction
%double has -1023 instead of -127
What you want is to round, which might affect the distribution of your values in subbtle and unintuitive ways.
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!