generate (not round) 2 decimal numbers
1 view (last 30 days)
Show older comments
Hello, in the first part of more complicated calculation I need some restriction to "rand" or so. My point is a reduction of unnecessary calculations and occurence of crowding results by simplification of the random number choice to 2 decimals in the first place. Is it possible?
Thank you
3 Comments
José-Luis
on 14 Sep 2016
The you need to use a format other than double. I am afraid "reducing" the number of decimals will do nothing for you, the number will still be stored as a double and will continue to use the same amount of memory.
Please explain what you are trying to achieve in order to help you better.
Answers (2)
Stephen23
on 14 Sep 2016
Edited: Stephen23
on 14 Sep 2016
Here are two methods of providing random numbers rounded to two decimal places:
>> randi([0,100],5)/100
ans =
0.8200 0.0900 0.1500 0.1400 0.6600
0.9100 0.2800 0.9800 0.4200 0.0300
0.1200 0.5500 0.9600 0.9200 0.8500
0.9200 0.9600 0.4900 0.8000 0.9400
0.6300 0.9700 0.8000 0.9600 0.6800
>> round(100*rand(5))/100
ans =
0.7600 0.7100 0.8200 0.4400 0.4900
0.7400 0.0300 0.6900 0.3800 0.4500
0.3900 0.2800 0.3200 0.7700 0.6500
0.6600 0.0500 0.9500 0.8000 0.7100
0.1700 0.1000 0.0300 0.1900 0.7500
Note that you also need to understand the behaviors of floating point numbers:
1 Comment
Walter Roberson
on 14 Sep 2016
Note: if memory space is a problem then you can use
randi([0,100], 5, 'uint8')
and adjust the rest of your algorithm to expect these to be 100 times too large. It probably will not help much in practice... it will probably lead to more problems than it is worth.
See Also
Categories
Find more on Creating and Concatenating Matrices 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!