Pick 3 lotto code
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
So I have to write a code that acts as a Pick 3 for the lotto. I chose 3 numbers and have to write a code that generates 3 random numbers 1000000 times and compares them to the 3 numbers I have chosen. The winning numbers must be in the same order as mine and the ratio must be 1 in 1000. Here is what I have so far.... and my professor says it's nowhere near right:
%Pick 3
%Fnding probability of getting 4 2 8 in a pick 3
clear all; clc; close all
A=0
N=1000000
for i=1:N
x=floor(9*rand(1)+1);
if (x==4)||(x==2)||(x==8)
A=A+1;
end
end
prob=A/N
fprintf('The number of times 4 2 8 were picked was %d , which had a probability of %f.\n',A,prob)
Please help.
1 Kommentar
Cedric
am 11 Sep. 2017
Type
doc randi
in the command window and see how that can help you.
If numbers is an array of integers in 1-9, e.g.
8 9 4
9 9 6
2 8 3
9 3 8
6 9 6
1 3 4
3 8 9
5 7 8
9 3 7
9 6 6
see if you can test whether elements of column 1 equal 4 for example, indexing all rows. See if you understand logical variables and logical operations like AND (&).
Maybe you will be able to build a vector of logicals whose elements are true (1) where all three column equal the triplet that you are looking for.
If you are able to build such a vector, logicals can be summed.
Finally, train with small values of N so you can easily display variables and understand what is happening. Pick a triplet that appears in your array of numbers and check that your approach is working. In the array that I gave above, you could pick [3, 8, 9] for example, for training.
Antworten (1)
James Tursa
am 11 Sep. 2017
Bearbeitet: James Tursa
am 11 Sep. 2017
Here is the instruction:
"... that generates 3 random numbers 1000000 times ..."
And here is your code:
N=1000000
for i=1:N
x=floor(9*rand(1)+1);
rand(1) will produce only one random number, not three random numbers. You need to use something like rand(1,3) instead. E.g.,
x=floor(9*rand(1,3)+1);
Then the numbers you generated will be in x(1), x(2), and x(3).
To get a match, you need all three numbers to match, not just one of the numbers. So your code:
if (x==4)||(x==2)||(x==8)
needs to change to compare if x(1) equals 4 AND x(2) equals 2 AND x(3) equals 8. So you need to turn my words into the proper code for this test. The "logical or" operator you are using is not what you want. You want to use the "logical and" operator instead.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Hypothesis Tests finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!