Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.36/5 (3 votes)
See more:
hi friends,

i want to know how Random class in c# generate random number. in fact i want to know structure of function Next(MinValue,MaxValue) that create random number.you know that all we know that it is not random in fact but developers of it show it to us like a random generator.

thanks.
Posted
Comments
[no name] 9-May-14 7:14am    
Okay so if you want to know how the Random class functions work, then there it nothing at all stopping you from downloading the source for the framework, finding the Random class and looking at it.
Meysam Toluie 9-May-14 7:17am    
+5
_Starbug_ 9-May-14 7:23am    
great persian read question carefully.
[no name] 9-May-14 7:32am    
We are reading it. You are not explaining anything.
_Starbug_ 9-May-14 7:24am    
no sir. you do not understand me.i am not a beginner in programming. i want the structure of Next(int,int) function of it. it generate a random number between min value and max value of parameters. but how it do this? what is the code for doing this? developers of next function what code places in the behind of next() function. i want this?

1 solution

As Wes says, look at the source.

I've just opened it up in dotPeek and it looks like this, although I think you'd need to study the whole class for context.

C#
public virtual int Next(int minValue, int maxValue)
{
  if (minValue > maxValue)
  {
    throw new ArgumentOutOfRangeException("minValue", Environment.GetResourceString("Argument_MinMaxValue", (object) "minValue", (object) "maxValue"));
  }
  else
  {
    long num = (long) maxValue - (long) minValue;
    if (num <= (long) int.MaxValue)
      return (int) (this.Sample() * (double) num) + minValue;
    else
      return (int) ((long) (this.GetSampleForLargeRange() * (double) num) + (long) minValue);
  }
}
 
Share this answer
 
Comments
Rob Philpott 9-May-14 7:31am    
Interesting that its marked virtual actually.
_Starbug_ 9-May-14 7:39am    
thanks. can you explain a bit about virtual keyword?i do not know anything about it.
Rob Philpott 9-May-14 7:44am    
virtual is a polymorphism construct. It says that you can create a class that derives from Random and override (replace) the next function with your own implementation.
_Starbug_ 9-May-14 7:52am    
in fact it is not actual. can be change.you can change it. i say this words from your words and virtual keyword itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900