I want to generate a random number between 1 and 10, but I want the chance of a 10 to be greater
1 view (last 30 days)
Show older comments
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 ?
0 Comments
Answers (3)
the cyclist
on 1 Dec 2016
Edited: the cyclist
on 1 Dec 2016
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]));
0 Comments
Star Strider
on 1 Dec 2016
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.
0 Comments
Roger Stafford
on 1 Dec 2016
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).
0 Comments
See Also
Categories
Find more on Switches and Breakers in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!