Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

How to generate many random various numbers?

, 12 Sep 2011
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.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralRe: (A) A byte is restricted to max. 256 distinct values because... PinmemberAnsgar Hellwig27-Sep-11 4:26 
GeneralRe: OK for the kiddy's @home [A] please explain why by using the... Pinmemberkribo27-Sep-11 3:36 
GeneralRe: I did not say that the maximum value is restricted to 256, b... PinmemberAnsgar Hellwig27-Sep-11 1:50 
GeneralRe: Then I dare you to run the next console application, written... PinmemberKenBonny27-Sep-11 1:47 
GeneralRe: Please have a closer look to the source code. He is using a ... PinmemberAnsgar Hellwig26-Sep-11 21:16 
GeneralRe: If I call RandomNumber(1024), what's the probability that it... PinmemberHenry.Ayoola19-Sep-11 0:55 
GeneralReason for my vote of 1 Doesn't work PinmemberShahin Khorshidnia14-Sep-11 21:37 
GeneralReason for my vote of 1 It restricts to 256 possible return ... PinmemberHenry.Ayoola13-Sep-11 3:03 
GeneralRe: OK you've lost me, say again...? Pinmemberkribo13-Sep-11 4:00 
GeneralRe: I don't see it either, unless I'm missing something. The va... PinmemberKenBonny14-Sep-11 0:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140905.1 | Last Updated 13 Sep 2011
Article Copyright 2011 by kribo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid