Click here to Skip to main content
12,079,202 members (37,199 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4K views
Posted

How to generate many random various numbers?

, 12 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Found this little peace of code in book and kept it.using System.Security.Cryptography;public static int RandomNumber(int maxvalue){ RandomGenerator random = RandomGenerator.Create(); byte[] r = new byte[1]; random.GetBytes(r); double val = (double)r[0]/byte.MaxValue; ...
Found this little peace of code in book and kept it.

using System.Security.Cryptography;
public static int RandomNumber(int maxvalue)
{
  RandomGenerator random = RandomGenerator.Create();
  byte[] r = new byte[1];
  random.GetBytes(r);
  double val = (double)r[0]/byte.MaxValue;
  int i = (int)Math.Round(val * maxvalue, 0);
  return i; 
}

Like I said not exactly my peace of code.
So to whom, thans..

License

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

Share

About the Author

kribo
Software Developer
Belgium Belgium
Developer within C#, Dynamics NAV (Navision), Php environments.

You may also be interested in...

Comments and Discussions

 
GeneralRe: (A) A byte is restricted to max. 256 distinct values because... Pin
Ansgar Hellwig27-Sep-11 5:26
memberAnsgar Hellwig27-Sep-11 5:26 
GeneralRe: OK for the kiddy's @home [A] please explain why by using the... Pin
kribo27-Sep-11 4:36
memberkribo27-Sep-11 4:36 
GeneralRe: I did not say that the maximum value is restricted to 256, b... Pin
Ansgar Hellwig27-Sep-11 2:50
memberAnsgar Hellwig27-Sep-11 2:50 
GeneralRe: Then I dare you to run the next console application, written... Pin
KenBonny27-Sep-11 2:47
memberKenBonny27-Sep-11 2:47 
GeneralRe: Please have a closer look to the source code. He is using a ... Pin
Ansgar Hellwig26-Sep-11 22:16
memberAnsgar Hellwig26-Sep-11 22:16 
GeneralRe: If I call RandomNumber(1024), what's the probability that it... Pin
Henry.Ayoola19-Sep-11 1:55
memberHenry.Ayoola19-Sep-11 1:55 
GeneralReason for my vote of 1 Doesn't work Pin
Shahin Khorshidnia14-Sep-11 22:37
memberShahin Khorshidnia14-Sep-11 22:37 
GeneralReason for my vote of 1 It restricts to 256 possible return ... Pin
Henry.Ayoola13-Sep-11 4:03
memberHenry.Ayoola13-Sep-11 4:03 
GeneralRe: OK you've lost me, say again...? Pin
kribo13-Sep-11 5:00
memberkribo13-Sep-11 5:00 
GeneralRe: I don't see it either, unless I'm missing something. The va... Pin
KenBonny14-Sep-11 1:05
memberKenBonny14-Sep-11 1:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160212.1 | Last Updated 13 Sep 2011
Article Copyright 2011 by kribo
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid