ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Random Number Generator (https://www.excelbanter.com/excel-programming/362248-random-number-generator.html)

[email protected]

Random Number Generator
 
I need VB code to Randomly generat numbers from 1 to 52. Can anybody
help?


Jason Zischke

Random Number Generator
 
Hi

Try this :

Dim MyValue
Sub Random()
MyValue = Int((52 * Rnd) + 1) ' Generate random value between 1 and 52.
End Sub


Jason


" wrote:

I need VB code to Randomly generat numbers from 1 to 52. Can anybody
help?



Tom Ogilvy

Random Number Generator
 
num = int(rnd()*52+1)

Are you trying to shuffle a card deck?


Sub Shuffle()
'
' Algorithm from:
' The Art of Computer Programming: _
' SemiNumerical Algorithms Vol 2, 2nd Ed.
' Donald Knuth
' p. 139 [Algorithm P]
'
'
Dim list As Variant
Dim rng As Range
Dim t As Long, j As Long, k As Long
For j = 1 To 52
Cells(j, 1) = j
Next j
Set rng = Range("A1:A52")
list = rng.Value
t = UBound(list, 1)
j = t
Randomize
For i = 1 To t
k = Int(Rnd() * j + 1)
lngTemp = list(j, 1)
list(j, 1) = list(k, 1)
list(k, 1) = lngTemp
j = j - 1
Next
rng.Value = list
End Sub



--
Regards,
Tom Ogilvy




wrote in message
oups.com...
I need VB code to Randomly generat numbers from 1 to 52. Can anybody
help?





All times are GMT +1. The time now is 02:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com