Answer by Sachin Ganjare
on 25 Jan 2013

Edited by Jan
on 25 Jan 2013

out = rand(1, 1);

decimal_out = floor(out * 100000000);

decimal_out = decimal_out\100000000;

Cy
on 26 Oct 2015

Don't you mean "decimal_out/100000000"?

Thorsten
on 26 Oct 2015

Your're right, that's a typo, must read /. Of course you can do it more efficiently in one line (for example, to generate a row vector of four random values):

x = floor(rand(1,4)*1e10)/1e10;

Sign in to comment.

Answer by Jan
on 25 Jan 2013

What about using rand() directly, because it satisfies: at least 8 decimal digits already?

Jan
on 25 Jan 2013

The limitation to 4 digits appears in the command line only, see: help format.

format long g

disp(rand(2,2))

Although I assume your code is just an abbreviated example, I recommend not to use "min" and "max" as names of variables, because this shadows frequently used Matlab commands.

However, I still do not understand your question. What does "fixing the number of decimal points" mean? RAND does reply values like 0.00000000001 also, so what is the problem.

Sign in to comment.

Answer by Evgeny Pr
on 25 Jan 2013

Edited by Evgeny Pr
on 25 Jan 2013

Do you have the distribution law for pseudo-random numbers for your problem? You can use RAND function for generate uniformly distributed numbers and define its non-linear (or linear) transformation function:

numbers = rand(10);

transformedNumbers = transform_function(numbers);

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Jan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/59849-decimal-random-number-generator#comment_125490

Sign in to comment.