Multiple Random Number Generator in Range

cdill

New Member
Joined
Jul 10, 2017
Messages
29
I'm doing a card draw simulation. I need to draw 8 random cards (with no duplicates) out of 52. I'll include my code below which is giving me a Run Time 28 error (Out of Stack Space). I am pretty sure my problem exists where I actually have Subs that call to themselves. I am sure there is an easier way to do this, but couldn't think of one.

Hopefully this is pretty clear what my intentions were. I just wanted to generate 8 integers (CARD1 - CARD8) out of a range between 1-52 (a deck of cards) and then display them in cells A1-A8.

Any help is appreciated.

Code:
   Dim CARD1 As Integer   
   Dim CARD2 As Integer
   Dim CARD3 As Integer
   Dim CARD4 As Integer
   Dim CARD5 As Integer
   Dim CARD6 As Integer
   Dim CARD7 As Integer
   Dim CARD8 As Integer


Sub DRAW8CARDS()
CARD1RAND
CARD2RAND
CARD3RAND
CARD4RAND
CARD5RAND
CARD6RAND
CARD7RAND
CARD8RAND
Sheets("Sheet1").Range("A1").Value = CARD1
Sheets("Sheet1").Range("A2").Value = CARD2
Sheets("Sheet1").Range("A3").Value = CARD3
Sheets("Sheet1").Range("A4").Value = CARD4
Sheets("Sheet1").Range("A5").Value = CARD5
Sheets("Sheet1").Range("A6").Value = CARD6
Sheets("Sheet1").Range("A7").Value = CARD7
Sheets("Sheet1").Range("A8").Value = CARD8
End Sub


Sub CARD1RAND()
CARD1 = Int((52 - 1 + 1) * Rnd + 52)
End Sub
Sub CARD2RAND()
CARD2 = Int((52 - 1 + 1) * Rnd + 52)
    If CARD2 = CARD1 Then
    CARD2RAND
    End If
End Sub
Sub CARD3RAND()
CARD3 = Int((52 - 1 + 1) * Rnd + 52)
    If CARD3 = (CARD1) Or (CARD2) Then
    CARD3RAND
    End If
End Sub
Sub CARD4RAND()
CARD4 = Int((52 - 1 + 1) * Rnd + 52)
    If CARD4 = (CARD1) Or (CARD2) Or (CARD3) Then
    CARD4RAND
    End If
End Sub
Sub CARD5RAND()
CARD5 = Int((52 - 1 + 1) * Rnd + 52)
    If CARD5 = (CARD1) Or (CARD2) Or (CARD3) Or (CARD4) Then
    CARD5RAND
    End If
End Sub
Sub CARD6RAND()
CARD6 = Int((52 - 1 + 1) * Rnd + 52)
    If CARD6 = (CARD1) Or (CARD2) Or (CARD3) Or (CARD4) Or (CARD5) Then
    CARD6RAND
    End If
End Sub
Sub CARD7RAND()
CARD7 = Int((52 - 1 + 1) * Rnd + 52)
    If CARD7 = (CARD1) Or (CARD2) Or (CARD3) Or (CARD4) Or (CARD5) Or (CARD6) Then
    CARD7RAND
    End If
End Sub
Sub CARD8RAND()
CARD8 = Int((52 - 1 + 1) * Rnd + 52)
    If CARD8 = (CARD1) Or (CARD2) Or (CARD3) Or (CARD4) Or (CARD5) Or (CARD6) Or (CARD7) Then
    CARD8RAND
    End If
End Sub
 
Last edited:
You might want to add a Randomize statement to your code, ..
Good point Rick, thanks - slipped my mind.

I need that output number be in row not in column.
How can I modify your code?
I would dimension my array differently, which also alters some of the other lines a bit. Try

Code:
Sub DrawCards()
  Dim AllCards(1 To 52) As String, DrawnCards() As String
  Dim i As Long, k As Long
  Dim tmp As String
  
  Const NumCards As Long = 8 '<- Change this if you want a different number of cards drawn
  
  Randomize
  ReDim DrawnCards(1 To NumCards)
  For i = 1 To 52
    AllCards(i) = "CARD" & i
  Next i
  For i = 1 To NumCards
    k = 1 + Int(Rnd() * (53 - i))
    tmp = AllCards(k)
    DrawnCards(i) = tmp
    AllCards(k) = AllCards(53 - i)
  Next i
  Range("A1").Resize(, NumCards).Value = DrawnCards
End Sub
 
Last edited:
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Good point Rick, thanks - slipped my mind.

I would dimension my array differently, which also alters some of the other lines a bit. Try

Rich (BB code):
Sub DrawCards()
  Dim AllCards(1 To 52) As String, DrawnCards() As String
  Dim i As Long, k As Long
  Dim tmp As String
  
  Const NumCards As Long = 8 '<- Change this if you want a different number of cards drawn
  
  Randomize
  ReDim DrawnCards(1 To NumCards)
  For i = 1 To 52
    AllCards(i) = "CARD" & i
  Next i
  For i = 1 To NumCards
    k = 1 + Int(Rnd() * (53 - i))
    tmp = AllCards(k)
    DrawnCards(i) = tmp
    AllCards(k) = AllCards(53 - i)
  Next i
  Range("A1").Resize(, NumCards).Value = DrawnCards
End Sub
If you wouldn't mind addressing your AllCards array as a two-dimensional array where the second dimension would always be 1, you can replace the highlighted loop with this single line of code...

AllCards = ["CARD"&ROW(1:52)]

If you want to continue addressing your AllCards array as a one-dimensional array (like you have it now), then you could use this single line of code instead...

AllCards = [TRANSPOSE("CARD"&ROW(1:52))]

In either case, you would have to change the AllCards variable to a Variant data type (with no parentheses).
 
Upvote 0

Forum statistics

Threads
1,215,701
Messages
6,126,289
Members
449,308
Latest member
VerifiedBleachersAttendee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top