Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

6 different random numbers

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
your help is much appreciated
1 Solution
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

Private Sub Form_Load()
   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 & " "
      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

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

Generated(1) = GenerateNumbers
If Generated(0) = Generated(1) Then
GoTo Generate1
End If

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

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

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

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

Generate = Int((intMax - intMin + 1) * Rnd + intMin)
GenerateNumbers = Generate
End Function
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!

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.

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


Homework, huh.
Programs don't need loops, that's for simplifications. They do need the if stmt, so use it for each 'case'
dim r(5) as integer

juz kidding...^_^
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now