nobody
nobody

Reputation: 8263

Random Number in Octave

I need to generate a random number that is between .0000001 and 1, I have been using rand(1) but this only gives me 4 decimal points, is there any other way to do this generation?

Thanks!

Upvotes: 2

Views: 1002

Answers (3)

RexFuzzle
RexFuzzle

Reputation: 1432

Setting the output precision won't help though because the number can still be less than .0000001 in theory but you will only be displaying the first 7 digits. The simplest way is:

req=0;
while (req<.0000001)
 req=rand(1);
end

It is possible that this could get you stuck in a loop but it will produce the right number. To display all the decimals you can also use the following command:

format long

This will show you 15 decimal places. To switch back go:

formay short

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881423

From the Octave docs:

By default, Octave displays 5 significant digits in a human readable form (option ‘short’ paired with ‘loose’ format for matrices).

So it's probably an issue with the way you're printing the value rather than the value itself.

That same page shows the other output formats in addition to short, the one you may want to look in to is long, giving 15 significant digits.

And there is also the output_precision which can be set as per here:

old_val = output_precision (7)
disp (whatever)
old_val = output_precision (old_val)

Upvotes: 4

SAlexandru
SAlexandru

Reputation: 11

Set the output_precision to 7 and it should be ok :)

Upvotes: 0

Related Questions