-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerate_Coupons01
46 lines (31 loc) · 1.51 KB
/
Generate_Coupons01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"This R script is used to randomly generate 6 numbers from 1 to 49, which
I call as a coupon and compare them to the previous winning numbers. If
there are more than 2 common numbers between the newly generated coupon,
and previous winning numbers/coupons from a designated database the
generated coupon will not be accepted. The generation of numbers will
continue until a preset value for the allowed number of coupons is reached.
The coupons generated will be stored in a matrix, and the numbers in each
coupon will be sorted in ascending order. "
lnumbers_hist = c(1,1,1,1,1,1)
repeat{
itera=0
i= 1
lnumbers<- sample(col1, 6) # randomly sample 6 number from 1 to 49
numrow=nrow(mydata2) # number of rows in the lotto-numbers matrix
if(numrow > 15){break} # set max allowed number of coupons generated
for (i in 1:numrow){
diffvect = setdiff(mydata2[i,],lnumbers) # compare rows to find different values
if(length(diffvect)< 4){ # if more than two numbers are the same for the compared rows, break the loop
itera = 1
print("+1")
break
}
}
if(itera ==1){
lnumbers_hist =c(lnumbers, lnumbers_hist) # storage for prognosis/debugging
}
else{
mydata2<-rbind(mydata2, rep(lnumbers)) # add new row to the existing matrix
mydata2 = t(apply(mydata2, 1, sort)) # sort rows of the matrix independent of each other
}
}