Advertisement
kromm77

[C#] Generazione numero random automatico

Dec 21st, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5. public static void Main()
  6. {
  7. Console.WriteLine("Hello World");
  8. var rnd = new ExponentialRandom();
  9.  
  10. var len = 16;
  11. var l1 = len / 2;
  12. var l2 = len - l1;
  13. Console.WriteLine(" L1 "+l1+ " L2 "+l2 );
  14. var nu = CreateANumber(rnd, l1 , l2 );
  15.  
  16. Console.WriteLine("0123456789abcdef" );
  17. Console.WriteLine("ea2345c7891b6d0f" );
  18. Console.WriteLine("0123012301230123" );
  19. Console.WriteLine(nu);
  20. var a = 10;
  21. var b = 11;
  22. var c = 12;
  23. var d = 13;
  24. var e = 14;
  25. var f = 15;
  26. var pos = new int[16] {
  27. e,
  28. a,
  29. 2,
  30. 3,
  31.  
  32. 4,
  33. 5,
  34. c,
  35. 7,
  36.  
  37. 8,
  38. 9,
  39. 1,
  40. b,
  41.  
  42. 6,
  43. d,
  44. 0,
  45. f
  46. };
  47.  
  48. var nun = "0123456789123456";
  49. var newnu = new System.Text.StringBuilder();
  50. for(var i = 0; i <16; i++ ) {
  51. newnu.Append(nun.Substring(pos[i],1));
  52. }
  53. Console.WriteLine(newnu);
  54.  
  55. }
  56. public static long CreateANumber(ExponentialRandom rnd, int l1, int l2)
  57. {
  58. var first = rnd.GetRandom(l1);
  59. var second = rnd.GetRandom(l2);
  60. Console.WriteLine(" first 01234567 second 01234567");
  61. Console.WriteLine(" first "+first+ " second "+second );
  62. Console.WriteLine(" L1 "+l1+ " L2 "+l2 );
  63. return (long)(first * Math.Pow(10, l2) + second);
  64. }
  65.  
  66.  
  67. }
  68. public class MyUintRandom
  69. {
  70. private static System.Random _rnd = new System.Random();
  71.  
  72. public MyUintRandom(System.Random rnd)
  73. {
  74. _rnd = rnd;
  75. }
  76.  
  77. public uint GetRandom(uint max)
  78. {
  79. var middle = (int) max / 2;
  80. var first = _rnd.Next(middle);
  81. var second = _rnd.Next(middle);
  82. return (uint) (first + second);
  83. }
  84. }
  85. public class ExponentialRandom
  86. {
  87. private static System.Random _rnd;
  88. private static MyUintRandom _myRnd;
  89.  
  90. public ExponentialRandom()
  91. : this(new System.Random())
  92. {
  93. }
  94.  
  95. public ExponentialRandom(System.Random rnd)
  96. {
  97. _rnd = rnd;
  98. _myRnd = new MyUintRandom(_rnd);
  99. }
  100.  
  101. public uint GetRandom(uint exp)
  102. {
  103. var maxValue = (uint) Math.Pow(10, exp);
  104. return _myRnd.GetRandom(maxValue);
  105. }
  106.  
  107. public int GetRandom(int exp)
  108. {
  109. var maxValue = (int) Math.Pow(10, exp);
  110. return _rnd.Next(maxValue);
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement