Random number

  • Thread starter Thread starter Steve Peterson
  • Start date Start date
S

Steve Peterson

Hi

Probably a dumb question, but I need to generate a random number between 0
and 9. I was hoping that someone could help out with a code snipplet on how
to do this.

TIA
Steve Peterson
 
This will do the job buddy. Happy Thanksgiving!

'Function to generate random numbers

Public Function GetRandomNumber() As Integer


Dim objRandom As New System.Random()


Return objRandom.Next(1, 9)

End Function
 
Leon said:
'Function to generate random numbers

Public Function GetRandomNumber() As Integer
Dim objRandom As New System.Random()
Return objRandom.Next(1, 9)
End Function


As a bit of clarification, this routine should not be creating
the random object in the function. The random object itself
should be created at a larger scope (Form or application wide)
because creating the generator like that causes it to use a
time-dependant default seed value which will be less 'random'
that creating the generator once, and calling it repeatedly for
the different numbers.

From the Remarks section in Help (Random Class)

"To improve performance, create one Random to generate many
random numbers over time, instead of repeatedly creating a new
Random to generate one random number."

LFS
 
Thx Leon!!

Happy Thankgsgiving to you to!


Leon said:
This will do the job buddy. Happy Thanksgiving!

'Function to generate random numbers

Public Function GetRandomNumber() As Integer


Dim objRandom As New System.Random()


Return objRandom.Next(1, 9)

End Function
 
This will do the job buddy. Happy Thanksgiving!

'Function to generate random numbers

Public Function GetRandomNumber() As Integer


Dim objRandom As New System.Random()


Return objRandom.Next(1, 9)

End Function

Actually... Since the OP requested between 0 and 9, that would have to
be:

Return objRandom.Next (0, 10)

Since the value returned is:

minvalue >= value < maxvalue
 
minvalue >= value < maxvalue

Don't you mean:

minvalue <= value < maxvalue


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top