Max value from System.Random

Posted on 2005-03-25
Medium Priority
Last Modified: 2010-04-23

I need to find out the max value that the system can generate by using the following random function. I am using these codes. And it returns 2147483647. Can anyone explain why the max value looks like this.

Dim myRandom As New System.Random
Dim myMax As Double = myRandom.Next().MaxValue

'---------myMax = 2147483647

Thank you,

Question by:CRIIT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 13631152
the random class works around integer values.... the function MaxValue returns an integer value

The Integer data type is typically 32bit  (i.e. when not using the other Int types)... which has ranges (from the help file):

"The Int32 value type represents signed integers with values ranging from negative 2,147,483,648 through positive 2,147,483,647."

So regardless of whether you are assigning it to a double or single, or long... that's going to be your range... I think :P

Expert Comment

ID: 13631177
Random R = new Random();

will generate a random number between 1 and 100.

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13631313

You are almost correct.


This will return a value between 1 and 99 inclusive.


"Return Value = A 32-bit signed integer greater than or equal to zero and less than MaxValue."


Accepted Solution

jrscherer earned 200 total points
ID: 13635173
Your question is: Can anyone explain why the max value looks like this.

The answer to your question is:
the System.Random function returns a Integer data type.
The statement  "Dim myMax As Double = myRandom.Next().MaxValue"  has nothing to do with randomization. It returns simply the value .MaxValue of a 32 bit integer, which is a constant of 2147483647, equals to (2 ^ 31) -1

If you want to see the maximum value a randomizer produces, you have to program a randomizer in a loop, run it for some time and memorize the highest value the randomizer did produce.



Author Comment

ID: 13644014
Thanks a lot

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question