鼎鼎知识库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12.random.md 993B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. > Random()随机函数
  2. ```
  3. Random ran=new Random();
  4. int RandKey=ran.Next(100,999);
  5. ```
  6. > 使用两种方式初始化一个随机数发生器
  7. -  第一种方法不指定随机种子,系统自动选取当前时间作为随机种子:
  8.   Random ro = new Random();
  9. -  第二种方法可以指定一个int型参数作为随机种子:
  10. ```
  11.   int iSeed=10;
  12.   Random ro = new Random(10);
  13.   long tick = DateTime.Now.Ticks;
  14.   Random ran = new Random((int)(tick & 0xffffffffL) | (int) (tick >> 32));
  15. ```
  16. ```
  17. 用随机数实现一件事情出现的概率是10%,另一件事情出现的概率是90%
  18. private staticvoidMain(string[] args)
  19. {
  20. Random ran = 
  21. newRandom(unchecked((int)DateTime.Now.Ticks));
  22. int num1 = 0;
  23. int num2 = 0;
  24. for (inti = 0; i<100000; i++) 
  25. {
  26. int n = ran.Next(0, 10);
  27. if (n == 0)
  28. {
  29. num++;
  30. }
  31. else
  32. {
  33. num2++;
  34. }
  35. }
  36. Console.Write(num1 + "--" + num2);
  37. }
  38. ```