Select a random number from a set

220 vues (au cours des 30 derniers jours)
Matt Medley
Matt Medley le 8 Nov 2011
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 commentaires
Walter Roberson
Walter Roberson le 8 Jan 2021
T1 = randperm(9)
T1 = 1×9
9 5 7 2 1 6 8 4 3
reshape(T1, 3, 3)
ans = 3×3
9 2 8 5 1 4 7 6 3
T2 = randperm(9)
T2 = 1×9
1 6 7 3 8 9 2 5 4
reshape(T2, 3, 3)
ans = 3×3
1 3 2 6 8 5 7 9 4
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 = 3×3
11 12 13 21 22 23 31 32 33
T3(randi(numel(T3)))
ans = 22
T3(randi(numel(T3)))
ans = 21
Nour Ahmed
Nour Ahmed le 9 Jan 2021
Yesss thankk youuu

Connectez-vous pour commenter.

Réponses (2)

Lucas García
Lucas García le 8 Nov 2011
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 commentaires
N/A
N/A le 29 Jan 2019
here is a way to do it :
for i = 1:5
arrayA(i) = randi(length(A));
arrayB(i) = randi(length(B));
end
Vineeth Krishnan
Vineeth Krishnan le 11 Déc 2020
for i=1:5
arrayA(i) = A(randi(length(A));
arrayB(i)= B(randi(length(B));
end

Connectez-vous pour commenter.


Souarv De
Souarv De le 8 Juin 2021
rand_Pos = randperm(length(x),1)
card = x(rand_Pos)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by