"Precise" random variables from a distribution
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Dear community,
I have an interval from 0 to 1 with a step 0.01 (also can be considered as from 0 to 100). This 101 values unifromly distruted on this interval.
I am trying to randomly draw two values from this interval and expect them to look like 0.15 and 0.25 or 0.9 and 0.56, etc.
I use the following code:
b = 1;
N = 0:0.01:1;
for i = 0:0.01:1
p1 = rand;
p2 = rand;
end
This gives me two random values between 0 and 1 but they have a smaller step, for instance, it returns p1 = 0.0538 and p2 = 0.7781.
Any help will be highly appreciated!
0 Commenti
Risposta accettata
Steven Lord
il 8 Ago 2022
Since 0.01 cannot be exactly represented in double precision, your values may not be exactly multiples of one-hundredth. But if you want to draw elements from a specific set of values, you can use randperm (without replacement) or randi (with replacement) to generate indices into that set.
S = [0, 5, 42, -999];
withoutReplacement = randperm(numel(S), 3) % Ask for 3 of the elements of S
withReplacement = randi(numel(S), [1 3])
S(withoutReplacement)
S(withReplacement)
11 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!