how can i generate the random numbers for specific probabilites

1 visualización (últimos 30 días)
Hello everyone.
I need to choose a random number for each probablitiy. The code is in the attachment. The code is perfect, i just need a random number for each probability.
Its really confusing for me. So if somebody can help me to solve ths problem, i will be very thankfull..
Thanks
  3 comentarios
waqas muhammad
waqas muhammad el 19 de Oct. de 2019
This is the code. if you look at the code. I have pt in the code and R denotes th random number [0,1]. if i take the probability of pt, it will give me some values, now for each value i need one random number .... if you see in the photo. this is just an example.. there are some values for probabilty (column 2) and the are also some values for random numbers column (4). basically the random number are from 0-100 in this photo. now for 0.01 probability it will give me a random number of 0, for probability 0.15,it will give me a random number bewtween 1-15, for 0.20 probability it will give me a random number between 16-35 as written in the table. in this way for each probability value i will get one random number..
Now in my code the range of random number is [0,1]...
Daniel M
Daniel M el 19 de Oct. de 2019
I still don't know what you want. Your code does not make that easier. Do you want to generate random numbers between two integers? That's very easy.
N = 100; % generate N random numbers
rnge = [1 15]; % on this interval
nums = randi(rnge,1,N); % generate random integers
nums2 = rand(1,N)*14 + 1; % generate random (non-integer) numbers in rnge

Iniciar sesión para comentar.

Respuesta aceptada

Bruno Luong
Bruno Luong el 19 de Oct. de 2019
Editada: Bruno Luong el 19 de Oct. de 2019
v = [ 0, 15, 25, 35, 45, 20]; % values
P = [0.01, 0.15, 0.20, 0.50, 0.12, 0.02]; % the correspond probability
c = cumsum([0 P]);
c = c / c(end); % make sure the cumulative sum terminates exactly with 1
n = 100; % length of the random sequence
[~,i] = histc(rand(1,n),c);
r = v(i)
  1 comentario
waqas muhammad
waqas muhammad el 21 de Oct. de 2019
bundle of thanks Sir.
Your comment really helped me alot. i got idea from your comment and it was worked perfect.
Once again Thanks alot

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by