johnhattan

Simpler random class for Unity

Apr 18th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. public static class Rnd // randoms should be simple, dagnabbit
  2. { // C# is pretty lazy about uints, so I'm just using ints
  3.     private static readonly System.Random R = new System.Random();
  4.  
  5.     public static int Uint
  6.     { // returns a random int between 0 and 2,147,483,647
  7.         get
  8.         {
  9.             return R.Next();
  10.         }
  11.     }
  12.     public static int UIntUpTo(int max)
  13.     { // returns an integer between 0 and max
  14.         return R.Next(max);
  15.     }
  16.     public static int UIntUpToInclusive(int max)
  17.     { // returns an integer between 0 and max inclusive
  18.         return R.Next(max + 1);
  19.     }
  20.     public static int UIntBetween(int min, int max)
  21.     { // returns an integer between min and max
  22.         return R.Next(min, max);
  23.     }
  24.     public static int UIntBetweenInclusive(int min, int max)
  25.     { // returns an integer between min and max inclusive
  26.         return R.Next(min, max + 1);
  27.     }
  28.  
  29.     public static float Float
  30.     { // traditional random number between 0 and 1 exclusive.
  31.         get
  32.         {
  33.             return (float)R.NextDouble();
  34.         }
  35.     }
  36.     public static float FloatUpTo(float max)
  37.     { // returns a number between 0 inclusive and max exclusive
  38.         return (float)(R.NextDouble() * max);
  39.     }
  40.     public static float FloatBetween(float min, float max)
  41.     { // returns a number between min inclusive and max exclusive
  42.         return (float)(R.NextDouble() * (max - min)) + min;
  43.     }
  44.  
  45.     public static bool Bool
  46.     { // returns true or false (50% chance of true)
  47.         get
  48.         {
  49.             return (R.Next() & 0x01) == 0;
  50.         }
  51.     }
  52.     public static bool BoolWeighted(float chance = 0.5f)
  53.     { // returns true or false (chance% chance of true)
  54.         return (R.NextDouble() < chance);
  55.     }
  56.  
  57.     public static int Sign
  58.     { // returns 1 or -1 (50% chance of 1)
  59.         get
  60.         {
  61.             return ((R.Next() & 0x01) == 0) ? 1 : -1;
  62.         }
  63.     }
  64.     public static int SignWeighted(float chance = 0.5f)
  65.     { // returns 1 or -1 (chance% chance of 1)
  66.         return (R.NextDouble() < chance) ? 1 : -1;
  67.     }
  68.  
  69.     public static int Bit
  70.     { // returns 1 or 0 (50% chance of 1)
  71.         get
  72.         {
  73.             return R.Next() & 0x01;
  74.         }
  75.     }
  76.     public static int BitWeighted(float chance = 0.5f)
  77.     { // returns 1 or 0 (chance% chance of 1)
  78.         return (R.NextDouble() < chance) ? 1 : 0;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment