Random Number Raffle Generator

조회 수: 6 (최근 30일)
Callum McIntosh
Callum McIntosh 2021년 1월 29일
댓글: Callum McIntosh 2021년 1월 29일
Hi there,
Been a few years since I previously wrote any Matlab scripts so forgive me, and I also know there is quite a lot of topics already covering small parts of this, but I was hoping to collate them.
I am looking to create a simulation that firstly assigns a number of tickets "t" (each with a randomly generated number between two limits (ie 1 and 3000)) to the user. The simulation then runs a loop of 100000 iterations, where, during each iteration, the simulation should randomly generate one number between the two limits. The purpose of this being, everytime this random number (lets call it "Winning_ticket") matches one of the users tickets, a count increases.
I have got to a stage where I can get the script to assign me with ticket numbers, but cannot get it to check if the "Winning_ticket" is a member of my numbers. And it also doesnt count consequtively (it only produces a 0 or 1 as a result). Any help would be greatly appreciated!
The end goal is to automate this process for t=1 all the way to t=40, and then to graph the count of winning tickets.
num_runs = 100000;
max = 3000;
t = 5;
R = randperm(max,t);
Draw = zeros(1,num_runs);
count = 0;
for n = 1:num_runs
Winner(n) = randi([1 max]);
if ismember(Winner,R);
count = count + 1;
end
disp(count)
end
  댓글 수: 7
Callum McIntosh
Callum McIntosh 2021년 1월 29일
Nevermind think I got it :)
%% Parameters
m = 1000;
t = 5;
R = randperm(m,t);
%% While loop test
n = 1; %% n = iteration
nmax = 100000000; %% failsafe
count = 0; %% count = winning tickets
while (count < 3 && n < nmax) %% stop loop when matched "x" winning tickets
Winner = randi(m);
if ismember(Winner,R);
count = count + 1
else
end
n = n + 1
end
Callum McIntosh
Callum McIntosh 2021년 1월 29일
Now my next task is to graph out this result in comparision to is t=1, and thn t=2 etc.
If you have a quick solution please let me know, if not I will try to get familar with this process via tutorials. Many thanks for the help Rik !

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by