Join us on Slack!
Things are more like they are now than they have ever been before. - Dwight D. Eisenhower
Tuesday, March 19, 2019
 Need Help?
Members Online
Total Online: 107
Guests Online: 104
Members Online: 3

Registered Members: 111638
Newest Member: Vikas204
Latest Articles

Visual Basic Random Numbers

Arrow Image Tutorial on how to generate random numbers in visual basic.

In this tutorial I\'m gonna teach you how to generate random numbers.

Well firstly You should open up Visual Basic 6, double click on Standard EXE.

Ok so now what do we want to output the random numbers to?

Hmmm... Well maybe we should just put it on a label. Yes? Great

Put a Button and Label on your form.

Name the button: btnRnd
Name the label: lblRnd
Clear the caption on the label.

Double click on your button and heres something new to learn:

Dim num1 as integer

That dimensions the variable num1 as an integer. So no matter what THAT variable will be a number. If you assign num1 to be \"fish\" then its value is 0. Got it? Good.

Ok so once you\'ve typed that enter down a couple times and type:


That randomize\'s the following code.

Now enter down a couple more times and type:

num1 = int(Rnd * 101) + 1

That assigns a number between 1 and 101 to the variable num1. Rnd Generates a random number and the 101 part is the highest number you want the random number to be. The + 1 part is where you want it to start. If you dont care if it starts at 0 then dont put anything there.

Ok once you\'ve got that down type:

lblRnd = num1

That should generate your random number for you.

Now what can you do with random numbers? Well there are alot of options most of which you should figure out for yourself. You COULD create a game with it, like Magic Dice or a lotto game.

And remember that You can do as maney numbers as you want, you dont have to limit yourself to just num1.


Dim num1 as integer, num2 as integer, num3 as integer


num1 = int(Rnd * 101) + 1
num2 = int(Rnd * 10) + 1
num3 = int(Rnd * 32) + 1

lblRnd1 = num1
lblRnd2 = num2
lblRnd3 = num3


mozzeron October 21 2006 - 07:59:50
Thought I'd be the first to mention that there is no such thing as randomly generated numbers, except atomic decay. So the only true random number generator will be the quantum computers
adlezon October 21 2006 - 11:21:00
Ya. Nice article tho. And I'm kinda thinking humans can't even make a random #. At least I can't Pfft
Arto_8000on October 21 2006 - 15:48:12
Well you could at least add a function to generate random with bound ... Function Random_Number (int min, int max) Randomize Random_Number = (Rnd*(max-min)+min) End Function The synthax is probably wrong because i haven't done VB since a long time but the algo should be good.
Post Comment


You must have completed the challenge Basic 1 and have 100 points or more, to be able to post.