?
Solved

How to make a REAL random number...

Posted on 2003-02-19
8
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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