c# - Comment puis-je générer un nombre int aléatoire

Mots clés : c#randomc#

meilleur 4 Réponses c# - Comment puis-je générer un nombre int aléatoire

vote vote

100

Random rnd = new Random(); int month  = rnd.Next(1, 13);  // creates a number between 1 and 12 int dice   = rnd.Next(1, 7);   // creates a number between 1 and 6 int card   = rnd.Next(52);     // creates a number between 0 and 51 
vote vote

80

var random = new Random(); int randomnumber = random.Next() 
using (RNGCryptoServiceProvider rg = new RNGCryptoServiceProvider())  {      byte[] rno = new byte[5];         rg.GetBytes(rno);         int randomvalue = BitConverter.ToInt32(rno, 0);  } 
vote vote

73

//Function to get random number private static readonly Random getrandom = new Random();  public static int GetRandomNumber(int min, int max) {     lock(getrandom) // synchronize     {         return getrandom.Next(min, max);     } } 
vote vote

60

Random r = new Random(); int n = r.Next(); 

Questions similaires