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

x
Solved

# How to make a REAL random number...

Posted on 2003-02-19
Medium Priority
202 Views
Trying to create a totally randomized dice-roller program. On program load, numbers are always the same, i nthe same order (i.e.-  first roll 15, second 8, third 6, etc...) using this code...

Public Sub d20(ByVal X)
X = Int(Rnd * 20)
End Sub

Any ideas?
0
Question by:Jodah

LVL 8

Expert Comment

ID: 7984284
Use Randomize

Public Sub d20(ByVal X)
Randomize
X = Int(Rnd * 20)
End Sub
0

LVL 2

Accepted Solution

Chuckie_Ice earned 200 total points
ID: 7984402
I noticed that using the Rnd Command made the same set of numbers every time that you reran the program, so i decided to post my own:

Public Function RandomNum(Min As Integer, Max As Integer) As Integer

Dim x As Long, y As Long, z As Long, tempnum As Long
Dim flag As Integer
Dim i As Integer

x = Min
y = Max
z = 1
If z = 0 Then Exit Function
If z > 20 Then z = 20
If z > y - x + 1 Then
Exit Function
End If
Randomize
RandomNum = Int((y - x + 1) * Rnd + x)

For i = 2 To z
Do
flag = False
Randomize
tempnum = Int((y - x + 1) * Rnd + x)
If (InStr(Num_list, tempnum)) Then
flag = True
End If
Loop Until Not flag
Next
End Function

To use this function, you would do:

MyRandomNumber = RandomNum 1, 40

This will genererate a random number between 1 and 40
0

Expert Comment

ID: 7984958
X = round((Rnd * 20) + .5)
if the rnd part doesn't work you have a problem!

test it
for y = 1 to 10
msgbox ( Rdn)
next y
0

Expert Comment

ID: 7986798
Public Sub d20(ByVal X)
Randomize Timer
X = Int(Rnd * 20)+ 1 '+1 means you get 0
End Sub
0

Expert Comment

ID: 7986802
'slight typo with the last post...

Randomize Timer
X = Int(Rnd * 20)+ 1 '+1 means you wont get 0
0

Expert Comment

ID: 7993788
jpwiedenhoff,
Not strictly true, rnd always returns the median number, you need to use the randomize command a la List244 et al to get it to give properly random numbers from the range 8-P
0

Expert Comment

ID: 7996474
OK your right. didn't noticed that before. my applications were written in a way which must have somehow provided that even If  I didn't noticed that yet. But live is for learning or?
0

Author Comment

ID: 8053959
Thanks. Got it running smoothly now :).
0

## Featured Post

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month10 days, 13 hours left to enroll