There are many lottery games like Mega Millions (USA) or Lotto (Poland) where we can win a lot of money tipping six numbers. In this tutorial we will create a REALbasic City Millions lottery simulator using REALbasic (we will drawn six balls from a set of balls numbered from 1 through 49). Watch it, learn it, enjoy it!
This will be a very simple GUI for this app so add to Window1 one Listbox Control and one PushButton Control. Now in PuhButton Sub Action () enter:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Dim lotto() As Integer Dim i As Integer lotto = Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,1,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,47,48,49) lotto.Shuffle // shuffle numbers for i = 43 to Ubound(lotto) // 49 - 43 = 6 numbers to drawn ListBox1.Addrow Str(lotto(i)) // add result to LIstbox App.DoEvents(2000) //make 2 seconds pause before displaying the next result next |
OK, but someone can ask why we haven’t use Random Class to drawn a number like in example below.
1 2 3 4 5 6 | Dim r As New Random Dim i As Integer for i = 0 to 6 ListBox1.Addrow Str(r.InRange(0,49)) next |
The answer is easy… of course we can do this but there is a option to get the same number twice or even more. And when it goes about lottery when we remove a ball there is no way to get it second time. Why ? Because it’s not there anymore
PS. When you will win the Grand Prize it would be nice if you can share some money with my
Thx!
What a Lotto fun!
I think we could also say…
ListBox1.Addrow str(lotto.Pop)
…then the ball would really be gone!
Nice Stive, your example is even better.
If you don’t know what s going on the pop method removes the last element from the array and returns its value.