Solved

BASIC code for generate a random number

Posted on 2002-07-06
5
183 Views
Last Modified: 2010-05-02
what is the BASIC code for generate a random number with certain length?

Thanks
0
Comment
Question by:leekf
5 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7133686
what do you mean by "certain length"?  A Random number is an value between a MINIMUM aad a MAXIMUM (say a random value between 100 and 1000). The Rnd function will generate random values between 0 and 1,and if you then MULTIPLY that random value by an Integer, you can generate a RANDOM Integer between 0 and the size of that Integer.

Dim intMax as Integer
dim intRandom as Integer
itMax = 1000

intRandom = intMax * Rnd ' random integer between 0 and 1000

If yu want a randome nyumber between 500 and 1500, then simply add 500 to intRandom above

Arthur Wood
0
 
LVL 14

Expert Comment

by:ajexpert
ID: 7133690
This snippet generates the random number between 1 and 10




Public Function GenerateRandomNum() As Integer

Randomize   ' Initialize random-number generator.
GenerateRandomNum = Int((10 * Rnd) + 1)  

End Function

' Calling RandomNumber Function in
'Command Button Click Event
Private Sub Command1_Click()  
    Dim intRNum As Integer
    intRNum = GenerateRandomNum
    MsgBox intRNum
End Sub

CHeers
0
 
LVL 33

Expert Comment

by:hongjun
ID: 7133705
Perhaps you might wish to include Randomize call. The purpose of including this is to make sure that a set of newly generated randomized numbers will be generated whenever your program run. Meaning to say if Randomize is not included, the list of numbers generated will always be the same whenever program is re-1run.


Dim intCode As Integer
Dim intStart As Integer
Dim intEndPlusOne As Integer

Randomize
intStart = 1000
intEndPlusOne = 10000

' intCode will be between intStart and intEndPlusOne-1
' which is 1000 to 9999
intCode = Int((intEndPlusOne - intStart + 1) * Rnd + intStart)



hongjun
0
 
LVL 3

Expert Comment

by:schworak
ID: 7133772
Here is a really nice function that I use all the time for random numbers.



Public Function Rand(ByVal Minimum As Double, ByVal Maximum As Double, Optional IntegerOnly As Boolean = True) As Double
   '
   ' Generate a number that falls between the Minimum and Maximum
   '
   Dim N As Double

   Randomize
   N = (Rnd * (Maximum - Minimum)) + Minimum
   If IntegerOnly Then
      Rand = Int(N)
   Else
      Rand = N
   End If
End Function
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 20 total points
ID: 7134040
This generates a random number string with a certain length.  All digits will be present even if there are leading zeros.

Option Explicit

Private Sub Command1_Click()
    MsgBox fRandNumString(40)
End Sub

Public Function fRandNumString(ByVal lngLen As Long) As String
    Dim i As Long
    Randomize
    fRandNumString = Space(lngLen)
    For i = 1 To lngLen
        Mid$(fRandNumString, i, 1) = CStr(Int(10 * Rnd))
    Next i

   
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using Access 8 59
Add and format columns in vb6 7 47
Protecting vb6 & .Net code Obfuscation 18 95
Can we place a tooltip on the actual vb6 form 5 35
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now