I want to generate a random number between 1 and 10, but I want the chance of a 10 to be greater

1 次查看(过去 30 天)
I want to simulate some parts of BlackJack. For wich I need the dealer to draw a random card. Since a jack, a queen and a King are a 10. I have a 4/13 chance of getting a 10. How can I program the 10 to be (4/13) greater and still have it be random ?

回答(3 个)

the cyclist
the cyclist 2016-12-1
编辑:the cyclist 2016-12-1
If you have the Statistics and Machine Learning Toolbox, you can do it like this:
numberSamples = 100;
y = randsample(1:10,numberSamples,true,[1 1 1 1 1 1 1 1 1 4]/13);
Another simple way (that is very intuitive) would be to generate uniform random integers from 1 to 13, and then apply a max of 10.
You can do this as so ...
y = min(10,randi(13,[1 numberSamples]));

Star Strider
Star Strider 2016-12-1
Another approach:
Deck = repmat([1:10 10 10 10], 130, 1); % Create Deck
Deck = Deck(:); % Create Vector
Tally1 = accumarray(Deck(:), 1); % Proof (Delete)
Tens_Pct1 = Tally1(10)/sum(Tally1); % Proof (Delete)
idx = randperm(numel(Deck), 4); % Deal Cards Index (4 Cards)
Deal = Deck(idx); % Deal Cards
Tally2 = accumarray(Deal, 1); % Proof (Delete)
Tens_Pct2 = Tally2(10)/sum(Tally2); % Proof (Delete)
The ‘Proof’ lines simply demonstrate that the code does what it’s designed to do.

Roger Stafford
Roger Stafford 2016-12-1
For n draws, do this:
r = rand(n,1);
card = ceil(18*r-10*(r-1/2).*(r>=1/2));
There will be a fifty percent chance of drawing ten (10) through king (13).

类别

Help CenterFile Exchange 中查找有关 Switches and Breakers 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by