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


How to make a REAL random number...

Posted on 2003-02-19
Medium Priority
Last Modified: 2010-04-07
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?
Question by:Jodah

Expert Comment

ID: 7984284
Use Randomize

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

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
RandomNum = Int((y - x + 1) * Rnd + x)

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

To use this function, you would do:

MyRandomNumber = RandomNum 1, 40

This will genererate a random number between 1 and 40

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
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.


Expert Comment

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

Expert Comment

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

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

Expert Comment

ID: 7993788
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

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?

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question