MATLAB Answers

mr mo
0

Creating random values with 2 Variable-precision accuracy in Matlab

Asked by mr mo
on 4 Dec 2017
Latest activity Commented on by Star Strider
on 4 Dec 2017
Hi. I want to create some random values between 0.17 and 0.87 by using the unifrnd command. My question is how can I create values with 2 digits of precision accuracy ? For example I want to have
0.22 0.81 0.27
Thanks a lot.

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 4 Dec 2017
Edited by Star Strider
on 4 Dec 2017
 Accepted Answer

Either of these will work:
A = 0.17;
B = 0.87;
unirnd2 = round(unifrnd(A, B, 1, 10), 2); % With The Most Recent ‘round’ Funciton
unirnd2 = round(unifrnd(A, B, 1, 10)*100)/100; % With Earlier Versons Of The ‘round’ Funciton

  2 Comments

Sign in to comment.


Answer by Steven Lord
on 4 Dec 2017

Generate integer values then divide.
intValues = randi([17 87], 1, 1e5);
histogram(intValues, 'BinMethod', 'integers')
decimalValues = intValues/100;

  0 Comments

Sign in to comment.