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

x
  • 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
0
tecfeet
Asked:
tecfeet
1 Solution
 
GPrentice00Commented:
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 & " "
   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
 
AuguryCommented:
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
 
GPrentice00Commented:
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
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!

 
fl0ydCommented:
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
 
billiousCommented:
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
 
SunBowCommented:
Homework, huh.
Programs don't need loops, that's for simplifications. They do need the if stmt, so use it for each 'case'
0
 
AdunCommented:
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
 
CleanupPingCommented:
tecfeet:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

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