Library tutorials & articles
Number Functions
- Conversion Functions
- Random Numbers
- More...
Random Numbers
You can use Rnd and Randomize to generate random numbers. The Rnd Function uses the following syntax:
Rnd()
However, the Rnd Function returns a random number less than one. So, in order to get a whole number, you need to use the Int function. If you need a larger range than 0 or 1, you need to multiply the result.
The following code simulates a dice rolling:
Randomize
Dice1 = Int(5 * Rnd) + 1
Msgbox "You rolled a " & Dice1
the statement Int(5*rnd) generates a number from 0 to 5. You add one after as you cannot role a 0. The Int statement rounds up the number from the Rnd function, as the RND function returns a number less than 1. The Randomize statement initializes the Rnd function. Without this, each time you run your application you will get the same sequence of numbers.
To produce random integers in a given range, use this formula:
Int((upperbound - lowerbound + 1) * Rnd
+ lowerbound)
Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
sNum = "Year: 2000, date: 12"
' Text1 will contain 200012
Text1.Text = Val(sNum)
This code is flat-out wrong.
The VAL function searches the source string, converting the string to a numeric value until it reaches the first non-numeric character. Since this example BEGINS with a non-numeric character, the returned value will be zero (0).
Anywho, the last page is a bit wasteful, don't you think?
[edit] O_o I went there again and it had gone back to 9999! Hehe, I did it twice!
This thread is for discussions of Number Functions.