Advertisement
Doomfister

Randomizer Class

Nov 24th, 2020
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace Random
  6. {
  7.     public class Randomizer
  8.     {
  9.         private uint A;
  10.         private uint B;
  11.         private uint C;
  12.         private uint Next = 12345;
  13.  
  14.         public const uint M = 16777216;
  15.         public const int INum = 100;
  16.         public const uint Div = M / INum;
  17.  
  18.         public Randomizer()
  19.         {
  20.             Initialize();
  21.         }
  22.  
  23.         public void Initialize()
  24.         {
  25.             DateTime currentDate = DateTime.UtcNow;
  26.  
  27.             B = (uint)(currentDate.Ticks % 10000 + 1);
  28.             B *= (uint)(currentDate.Second);
  29.             B = (B % 2 != 0) ? B : B + 1;
  30.  
  31.             A = (uint)(currentDate.Ticks % 1000 + 1);
  32.             A += (uint)(currentDate.Millisecond % 500);
  33.             A = A * 4 + 1;
  34.  
  35.             C = (uint)(currentDate.Ticks % 100 + 800);
  36.             Next = C;
  37.         }
  38.  
  39.         public uint GenerateNext()
  40.         {
  41.             Next = (Next * A + B) % M;
  42.             return Next;
  43.         }
  44.  
  45.         public double[] GenerateSequence(int count)
  46.         {
  47.             var frequence = new double[INum];
  48.             var next = C;
  49.             var strBuilder = new StringBuilder();
  50.  
  51.             for (var i = 0; i < count; i++)
  52.             {
  53.                 next = GenerateFromPrev(next);
  54.                 strBuilder.Append(next);
  55.                 strBuilder.Append(' ');
  56.                 var interval = next / Div;
  57.                 if (interval >= INum)
  58.                     frequence[frequence.Length - 1]++;
  59.                 else
  60.                     frequence[interval]++;
  61.             }
  62.  
  63.             for (var i = 0; i < INum; i++)
  64.             {
  65.                 if (frequence[i] == 0)
  66.                     continue;
  67.                 else
  68.                     frequence[i] = Math.Round(frequence[i] / count * 100, 2,
  69.                         MidpointRounding.AwayFromZero);
  70.             }
  71.  
  72.             File.WriteAllText("output.txt", strBuilder.ToString());
  73.             return frequence;
  74.         }
  75.  
  76.         private uint GenerateFromPrev(uint prev)
  77.         {
  78.             return (prev * A + B) % M;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement