martin2k

Home | Forums | VB6 | Games | Software | Directory | HTML | Wiki | Blog | Contact

How do I generate a random number?


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

Question

From: Doherty Family
Date: Wednesday 07 February 2001 1:03 AM
Subject: <No subject>
Question: hey my name's mike and i was just wondering if you could post up like a randomizer project in project form.  My email address is and I just started Visual Studio programming (I'm 13).  Please contact me because I need a lot of help and you look like as though you can help me

What type of project do you want me to create for you? I have made a function for random numbers if that's what you mean:

Function Random(Lowerbound As Long, Upperbound As Long)
Randomize
Random = Int(Rnd * Upperbound) + Lowerbound
End Function


blog comments powered by Disqus

Archived Comments

From: Apolonio P. Patricio III
Date: Wednesday, December 15, 2010 at 14:52:17
Comments: thank you for the shorter syntax.. appreciate cheers!
 
From: Chris
Date: Friday, September 17, 2010 at 18:54:15
Comments: I have used this code before, and it seems to "favor" the mid range of numbers (if you are using 1-10, 3-7 are the most common):
 
Function Random(Lowerbound As Long, Upperbound As Long)
Randomize
Random = Int(Rnd * Upperbound) + Lowerbound
End Function
 
From: JOHN
Date: Friday, September 10, 2010 at 17:48:07
Comments: The randomizing are really working but for people giving the answers, try to be specific whether on button,form load and any.
 
From: mohamed <mohamed .aljarady1@gmail.com>
Date: Friday, June 4, 2010 at 00:28:33
Comments: thank you for this information and please i want from you to put more simple examples
 
From: Jarod
Date: Monday, May 3, 2010 at 14:32:11
Comments:
        TextBox1.Text = Int(Rnd() * Now.TimeOfDay.TotalSeconds)

or use the seconds
 

        TextBox1.Text = Int(Rnd() * Now.Second)

Or Millisecond depending on how high you want it to go
 

        TextBox1.Text = Int(Rnd() * Now.Millisecond)

You get different numbers every time.

 
From: Campbell
Date: Friday, September 11, 2009 at 07:59:54
Comments: (I'm a eight and I play around with VB.)
I'm having the same problem and you helped me solve it! Thanks!
 
From: Art
Date: Wednesday, July 8, 2009 at 23:52:06
Comments: This code does not create a random sequence; it repeats itself and some combinations do not have a chance or outcome at all:
 
Private Sub N_Integers(N As Integer, rndlist() As Integer)
Randomize
'Randomly sorts N integers and puts results in rndlist
Dim I As Integer, J As Integer, T As Integer
'Order all elements initially
For I = 1 To N
  rndlist(I) = I
Next I
'J is number of integers remaining
For J = N To 2 Step -1
  I = Int(Rnd * J) + 1
  T = rndlist(J)
  rndlist(J) = rndlist(I)
  rndlist(I) = T
Next J
End Sub
 
From: natti
Date: Friday, January 30, 2009 at 20:13:01
Comments:

I Think This One Is Easy, Simple And Works, All You Need To Do Is Make A Label (Label1) And Enter The Fourm Code: Enter:

Private Sub Form_Load()
Randomize
Label1.Caption = Int(Rnd * 10)
End Sub

Ok, The 10, The (Rnd * 10) Bit, Change The 10 To What Number The Generator Can Go Up To, Eg, 4 Digit Number, Enter 9999

 
From: stonie
Date: Thursday, October 2, 2008 at 06:39:27
Comments: Nice examples. Can be used to do other things too. Thanks!
 
From: sdfsdfsdfsdf
Date: Saturday, June 14, 2008 at 01:42:20
Comments: Do this 
Dim RandomNumber as Integer
Randomize
RandomNumber = Int(((UpperBound + 1) - Lowerbound) * Rnd + LowerBound)
 
From: M.G.Srinivasa
Date: Monday, May 12, 2008 at 09:50:32
Comments: Dear Mike

Thanks i made use of your code. i am developing a online testing software like the MCSE exam where the question paper gets randomly selected from the database. the code has helped a lot, i will be in touch with you
Thanking you
M.G.Srinivasa (INDIA)

 
From: Sew
Date: Saturday, August 19, 2006 at 07:28:03
Comments: In our LMS(Lottery Management) System we want to select winner,who's record number need to be randomly selected from the database.The  way of  selecting a record  from the DB not to be the same pattern .It must need to be a random selection.
 
From: g
Date: Saturday, September 3, 2005 at 12:54:18
Comments: Im looking for a way of genearting random numbers between one and nine such that the same number is not generated more than once. Is there any easy way of doing this or do i have to put the generated numbers in an array and have vb compare them??
 
Reply:

The answer is on this page: How do I generate a list of random numbers, which are all different?

 
From: jim
Date: Saturday, June 4, 2005 at 21:26:47
Comments: Need help!
Each time I put randomise, it says : compile error: sub or function not defined.
 
Reply:

You need to spell randomise the American way - 'Randomize'.

 
From: Cookie
Date: Saturday, January 8, 2005 at 20:13:41
Comments:

I have got the following code to produce a random dice value (1-6), however each time I run my program it gives me the same "random" numbers in the same order.

random(dice_no) = (Int((6 - 1) * Rnd) + 1)

Is there anyway to sort this?

 
Reply:

The following function will sort numbers into an array, so that each element has a different number:

Private Sub N_Integers(N As Integer, rndlist() As Integer)
Randomize
'Randomly sorts N integers and puts results in rndlist
Dim I As Integer, J As Integer, T As Integer
'Order all elements initially
For I = 1 To N
  rndlist(I) = I
Next I
'J is number of integers remaining
For J = N To 2 Step -1
  I = Int(Rnd * J) + 1
  T = rndlist(J)
  rndlist(J) = rndlist(I)
  rndlist(I) = T
Next J
End Sub

For example, if you set the first argument to 6, the elements of rndlist might be (starting at element 1): 2, 1, 4, 3, 6, 5.

 
From: Emma
Date: Tuesday, November 2, 2004 at 05:00:35
Comments: hi
i am currently having trouble generating random numbers for a maths programme i have to do for a school project. my current coding is 
NO1 = (Int(Rnd * 25) + 1)
NO2 = (Int(Rnd * 25) + 1)

and i have declared my variables and the random numbers are not showing

 
Reply:

To show the variables, they need to be displayed to the user e.g.:

MsgBox "Random number 1: " & NO1 & ".  Random number 2: " & NO2
 
From: Blizzy
Date: Saturday, July 31, 2004 at 02:58:36
Comments: Its

Randomize
text1.text = Int(Rnd * NUMBER)

 
From: Bob <UNAVALIABLE>
Date: Wednesday, December 24, 2003 at 02:36:29
Comments: Im making an RPG, i need to generate a random number. It needs to be from 1-26. I need that script to find what creature appears. Please help me! (PS: Im using Visual Basic 6.0)
 
Reply:

Use the function at the top of this page using numbers 1 and 26:

r = Random(1, 26)
 
From: Sohail Jamal
Date: Sunday, November 9, 2003 at 20:05:22
Comments: Guyz according to my calculations,the command must be in the following manner :

Randomize
VALUE=Int((HIGHESTNUMBER - LOWESTNUMBER) * Rnd + LOWESTNUMBER)

where VALUE is your variable
      HIGHESTNUMBER is the highest number for random generation
       LOWESTNUMBER is the lowest number for random generation

 
From: Daniel
Date: Monday, November 3, 2003 at 12:50:06
Comments:

just another way it can be done, a MUCH simpler way... though JUST for generating random numbers, nothing else... try:

Private Sub Form_Load()
Randomize
Label1.Caption = Int(Rnd * 10)
End Sub

10 can be replaced with any number you want, as high or low as you set it!

 
From: Joseph
Date: Saturday, September 27, 2003 at 11:04:23
Comments: Should be like this... i tested it
Function Random(Lowerbound As Long, Upperbound As Long)
Randomize
Random = Int((Upperbound - Lowerbound) * Rnd + Lowerbound)
End Function
 
From: Eddie
Date: Monday, July 14, 2003 at 23:59:57
Comments: My program randomly chooses integers then add them
n1+n2=n3. If I wanted the program to toggle from addition to substraction. Let's say! the first time it added the intergers and the next new integers are substracted. Someone suggested using Random(11,13)
I don't understand, do you have a better idea?
 
Reply:

I think the best way of doing this is to use a Boolean, which when True the numbers are added and when False, the numbers are subtracted.  E.g.:

Select Case AddNumbers
Case True
    n3 = n1 + n2
    AddNumbers = False
Case False
    n3 = n1 - n2
    AddNumbers = True
End Select

Martin Allen 1999 - 2011.  Last updated Tuesday 09 August 2011 08:13:56 PM +0100.