Random Number Raffle Generator

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 commentaires

Rik
Rik le 29 Jan 2021
What are you expecting ismember(R) to do? I would have to look up the documentation to see what it does with a single input.
In any case, ismember produces a logical array as output.
Why don't you reduce the number of iterations and the max value and step through your code to evaluate line by line what happens?
Callum McIntosh
Callum McIntosh le 29 Jan 2021
Modifié(e) : Callum McIntosh le 29 Jan 2021
Sorry you are right, edited the "ismember" now. It works, however it only gives Count = 1 for a match at any time, and Count = 0 for no matches...how do I get this to accumulate?
Also is there a way to change the for loop (with a defined set of iterations) for a while loop, so that I can have an accurate count of iterations before a match is discovered?
Rik
Rik le 29 Jan 2021
Every for-loop can be edited to be a while loop:
for n=a:b:c
%code
end
n=a;
while n<c
%code
n=n+b;
end
Also, check the output of the ismember function. Did you check the documentation for if to see what it does with array inputs?
You are also checking the entire winner array in ismember, instead of only the current iteration.
Callum McIntosh
Callum McIntosh le 29 Jan 2021
Modifié(e) : Callum McIntosh le 29 Jan 2021
I'm sorry, I really am rusty at this, could you perhaps provide the while loop for my example above, so I can understand more clearly?
Also how would you get it to check each iteration, ie. run script until it has matched the ticket numbers "R" with the Winning number x times (say 3 times for example).
I have this so far (but it spits out nonsense that I cant make sense of):
%% Parameters
m = 100; %% Max entries
t = 5; %% Players ticket amount
R = randperm(m,t); %% Random numbers assigned to player
%% While loop test
n = 1; %% n = iteration
count = 0; %% count = winning tickets
while count < 3 %%stop loop when matched 3 winning tickets
Winner(n) = randi(m);
if ismember(Winner,R);
n = n + 1
count = count + 1
end
disp(n)
end
Rik
Rik le 29 Jan 2021
To get back up to speed with Matlab I can recommend the Onramp tutorial.
Your code uses this as the for syntax: for n = 1:num_runs. So n is n, a is 1, b is 1, and c is num_runs. You can change the condition as you please.
n=1;
while n<num_runs
%your code inside the for loop goes here
n=n+1;
end
I would strongly suggest taking a look at what value you're using as a condition here to make sure you're counting a win correctly. I would suggest doing that before you switch to a while loop.
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 le 29 Jan 2021
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 !

Connectez-vous pour commenter.

Réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by