Select a random number from a set
이전 댓글 표시
I'm simulating a single blackjack hand and am trying to "draw" a card. I can't use the randi function because I don't want all possibilities to have the same probability of being selected. So what I have done is created a row vector , (x), of all the possible card values. Now I would like draw a random number from this selection for my 'draw' function. Thanks for the help!
x = [1,2,3,4,5,6,7,9,10,10,10,10,11] theCard = randi?
댓글 수: 5
Nour Ahmed
2021년 1월 8일
If i have a matrix 3x3 and i fill up them from 1 to 9 and want to choose randomley a number by computer how i can make this happen?
reshape(randperm(9), 3, 3)
Nour Ahmed
2021년 1월 8일
I mean i need the computer to pic a number randomly not reshaping the matrix
T1 = randperm(9)
reshape(T1, 3, 3)
T2 = randperm(9)
reshape(T2, 3, 3)
You can see that the randperm(9) call is picking values randomly, and the reshape is making a 3 x 3 out of it.
Or perhaps you mean that you have created an existing 3 x 3 matrix and you want to take a random value out of it?
T3 = [11 12 13
21 22 23
31 32 33]
T3(randi(numel(T3)))
T3(randi(numel(T3)))
Nour Ahmed
2021년 1월 9일
Yesss thankk youuu
답변 (2개)
Lucas García
2011년 11월 8일
There are a few ways to do it. For example, using randi to select in which position is the card that you will extract.
pos = randi(length(x));
card = x(pos);
댓글 수: 5
Matt Medley
2011년 11월 8일
Lucas García
2011년 11월 8일
By doing this: pos = randi(length(x));
you will be randomly choosing one of the positions in x.
Then by doing: card = x(pos);
you are retrieving the card placed in that position.
I'm not sure if this is exactly what you are looking for.
usman younus
2017년 6월 28일
편집: usman younus
2017년 6월 28일
A=[3818.36,7632.40,11446.44,15260.48,19074.52];
B=[657.15,1089.15,1521.15,1953.15,2385.15];
i have 2 arrays, I want to select randomly any 5 numbers from both arrays how can i select?
N/A
2019년 1월 29일
here is a way to do it :
for i = 1:5
arrayA(i) = randi(length(A));
arrayB(i) = randi(length(B));
end
Vineeth Krishnan
2020년 12월 11일
for i=1:5
arrayA(i) = A(randi(length(A));
arrayB(i)= B(randi(length(B));
end
Souarv De
2021년 6월 8일
rand_Pos = randperm(length(x),1)
card = x(rand_Pos)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!