Random

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can somebody help me out? I need the code to generate a random number between
1 and 100.
 
Dim randValue As Integer
Dim x As New Random
randValue = x.Next(1001)

The documentation on the Next method does not state the very important fact
that the value you pass as the maximum value the random class should return
(in this case 1001) is never reached. So, if you want to potentially get
back 1000, you must enter 1001.
 
Efrain said:
Can somebody help me out? I need the code to generate a random number
between
1 and 100.

Scott gave you VB code. Here's the C# equivalent.

Random myR = new Random();

int x = myR.Next(1,101);

The C++ code would depend on the type of project, i.e. the .NET Random class
would be appropriate in a .NET WinForms app, but not in a MFC app.

What language are you using?
 
Scott M. said:
Dim randValue As Integer
Dim x As New Random
randValue = x.Next(1001)

The documentation on the Next method does not state the very important fact
that the value you pass as the maximum value the random class should return
(in this case 1001) is never reached. So, if you want to potentially get
back 1000, you must enter 1001.

However, the lower bound should be 0 here, so actually what's wanted is

x.Next(100)+1

(or x.Next(1, 101))
 

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