How to create random number between (0....1]?

Could someone suggest me how to create random numbers between (0....1]?

Answers (2)

Read about rand.
iwant = rand(1,10)
iwant = 1×10
0.3795 0.0287 0.4453 0.7789 0.3209 0.6367 0.2754 0.1285 0.6886 0.7143

5 Comments

how about i want 6 floating numbers between 0 and 1 in which 0 being excluded and 1 being included
A = [rand(1,5) 1] ;
iwant = randsample(A,length(A))
iwant = 1×6
0.1765 1.0000 0.4112 0.6226 0.0652 0.4206
Here you r taking 5 random numbers and adding the element 1 in the sample. Is it possible to take 6 random numbers between 0 and 1 in which 1 is also included in the random number generated???
You put a condition that 1 should be included, so 1 is added...later the number are made random using randsample. You can give a try generating 6 random numbers with 1, you will not get it.
The user wants to generate from uniform random distribution of floating point numbers in which the source distribution excludes 0 but includes 1, with the possibility of 1 to have equal probability with any other possible output.
The user did not actually state how dense they need the distribution to be. If they would be happy with 2^52 or fewer numbers in the source set, then the calculations can be done fairly easily making use of randi.
If the user wants more 2^53*values in the source set, then it would be necessary to switch to a different representation, not ieee 754 double precision.
If the user wants 2^53 values in the source set, that is possible with a small bit of indirection.
The most commonly used uniform random number generator in MATLAB generates from a source set that is 2^53 - 1 values, one value fewer than is ideal for their needs .

Sign in to comment.

This takes work to get right.
format long g
N = 6;
temp = typecast(reshape([randi([0 (2^8-1)], 6, N, 'uint8'); randi([0 (2^5-1)], 1, N); zeros(1, N,'uint8')],[],1),'uint64')
temp = 6×1
6216815486836182 5783593910339116 5332195457436175 6764184342683364 8586546077226935 7892688937955983
iwant = double(temp) / flintmax;
iwant(iwant == 0) = 1
iwant = 6×1
0.690205169333178 0.642107912434033 0.591992616864731 0.750975320005605 0.953298115694216 0.876264498512301
You cannot use rand() because rand() excludes 0 and 1.
You cannot [directly] use randi() because randi has a maximum span of 2^53 - 2
So... what we do is generate a 53 bit binary number and pack it together into a uint64. A 53 bit binary number has 2^53 different possible values, from 0 to 2^53 - 1. We can double() the value because all integers 0 to 2^53 are directly representable in double precision. Then we divide by 2^53 to scale to 0 to (2^53-1)/2^53 . We then check for 0 and if we see it we substitute 1. So now we have a range of 2^53 different numbers that excludes 0 and includes 1.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 17 Aug 2021

Commented:

on 19 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!