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