Solved

# 6 different random numbers

Posted on 2003-03-30
Medium Priority
335 Views
How would you write a visual basic program to generate 6 random numbers (1 thru 52) making *sure* that they are all unique but, use *NO* loops at all. using only if statements

thank you
0
Question by:tecfeet
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 6

Accepted Solution

GPrentice00 earned 300 total points
ID: 8236856
This sounds more like a specific design challange than a practical approach!  Still, up for the challange, the best way to replace a loop:

Use a timer event trigger and a static counter, IF a condition is met, advance the static counter, else leave it alone, dont do anything.  The next time the timer triggers, code happens and maybe this time the counter gets advanced.  After the counter reaches its endpoint, disable the timer from firing.

-------------------------------------
' Form code
' put two text boxes and a timer onto a new form project
' to see this code in action.  Run it several times
' to see that it finds already used numbers and does it again
' I'd recommend capturing the numbers into an array, using
' numcounter as the index for real usage.  But then again, everyone
' here would say "Why do you need to exclude using a loop" when
' its the best and fastest way to do it for REAL usage...

Option Explicit

Randomize (Time)
Me.Timer1.Interval = 200 'milliseconds, can make smaller to make action invisible
' or make larger to see the action occuring.
Me.Text1 = "List of numbers= "
Me.Text2 = "Was already picked= "
Me.Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()
Dim x As Integer
' static flag array will be set true when already selected
Static NumSelected(52) As Boolean
Static NumCounter As Integer
x = Int(Rnd * 52) + 1     'pick an integer 1 to 52
If NumSelected(x) Then
Me.Text2 = Me.Text2 & x & " "
Else
NumCounter = NumCounter + 1
NumSelected(x) = True
Me.Text1 = Me.Text1 & x & " "
End If
If NumCounter = 6 Then
Me.Timer1.Enabled = False
Me.Text1 = Me.Text1 & " <--6 numbers"
End If
End Sub

0

Expert Comment

ID: 8236858
Why you don't want to use loops I will never know. However if you use a Goto statement (which I avoid as much as possible) your code my look like this:

Assuming you need to generate numbers 1-52 when Command1 is clicked.

Private Sub Command1_Click()
Dim Generated(0 To 5) As Integer

Generated(0) = GenerateNumbers

Generate1:
Generated(1) = GenerateNumbers
If Generated(0) = Generated(1) Then
List1.Clear
GoTo Generate1
End If

Generate2:
Generated(2) = GenerateNumbers
If Generated(2) = Generated(0) Or Generated(2) = Generated(1) Then
GoTo Generate2
End If

Generate3:
Generated(3) = GenerateNumbers
If Generated(3) = Generated(0) Or Generated(3) = Generated(1) Or Generated(3) = Generated(2) Then
GoTo Generate3
End If

Generate4:
Generated(4) = GenerateNumbers
If Generated(4) = Generated(0) Or Generated(4) = Generated(1) Or Generated(4) = Generated(2) Or Generated(4) = Generated(3) Then
GoTo Generate4
End If

Generate5:
Generated(5) = GenerateNumbers
If Generated(5) = Generated(0) Or Generated(5) = Generated(1) Or Generated(5) = Generated(2) Or Generated(5) = Generated(3) Or Generated(5) = Generated(4) Then
GoTo Generate5
End If

End Sub

Private Function GenerateNumbers()
Dim intMax As Integer
Dim intMin As Integer
Dim Generate As Integer

intMax = 52
intMin = 1

Randomize

Generate = Int((intMax - intMin + 1) * Rnd + intMin)

GenerateNumbers = Generate
End Function
0

LVL 6

Expert Comment

ID: 8236882
if by "only IF statements" you are even going as far as to exclude ELSE statements, then naturally what works is:

If NumSelected(x) Then
Me.Text2 = Me.Text2 & x & " "
End If
If Not NumSelected(x) Then
NumCounter = NumCounter + 1
NumSelected(x) = True
Me.Text1 = Me.Text1 & x & " "
End If

----------
if you mean something extreme that would have 60 IF statements nested inside each other as being the only ways to express this function, like those challanges that say "using only the number 2 and plus, minus, multiply, divide symbols, generate the numbers 1 to 100", then good luck!

0

LVL 8

Expert Comment

ID: 8236938
Hmmm, it is generally homework assignments that make those weird restrictions. In real world applications you generally don't have such prerequisites implied, so I would recommend to do your homework yourself.

.f
0

LVL 7

Expert Comment

ID: 8237937
Since you're asking in a Programming, not a VB area, I'll assume you want pseudocode.

Establish an array of 52 numbers, and fill the array with the numbers 1 to 52.

swap entry 1 with entry random(52)
swap entry 2 with entry random(51)+1
swap entry 3 with entry random(50)+2
swap entry 4 with entry random(49)+3
swap entry 5 with entry random(48)+4
swap entry 6 with entry random(47)+5

where random(n) means a random number between 1 and n, inclusive.

And the result will be a random sequence in entry 1..6 of the array, with NO loops, NO IFs, NO THENs and NO GOTOs

...Bill

0

LVL 24

Expert Comment

ID: 8241855
Homework, huh.
Programs don't need loops, that's for simplifications. They do need the if stmt, so use it for each 'case'
0

Expert Comment

ID: 8242755
dim r(5) as integer
r(1)=int(rnd*46)+1
r(2)=r(1)+1
r(3)=r(2)+1
r(4)=r(3)+1
r(5)=r(4)+1
r(6)=r(5)+1

juz kidding...^_^
0

Expert Comment

ID: 9446648
tecfeet:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress
###### Suggested Courses
Course of the Month14 days, 3 hours left to enroll