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
8
Medium Priority
?
202 Views
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?
0
Comment
Question by:Jodah
8 Comments
 
LVL 8

Expert Comment

by:List244
ID: 7984284
Use Randomize

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

Accepted Solution

by:
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

by:jpwiedenhoff
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
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

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

Expert Comment

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

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

Expert Comment

by:griffithsk
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

by:jpwiedenhoff
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

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

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