aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
authorCinder2015-05-27 08:55:49 -0600
committerDiva Canto2015-05-27 11:15:09 -0700
commit0af17c94848724c0d56d4be12c01983b4793b8bf (patch)
treec28c9f82c7d3d801bcc6d4d6dc41d619733ecf9f /OpenSim/Framework
parentMerge branch 'master' of ssh://opensimulator.org/var/git/opensim (diff)
downloadopensim-SC_OLD-0af17c94848724c0d56d4be12c01983b4793b8bf.zip
opensim-SC_OLD-0af17c94848724c0d56d4be12c01983b4793b8bf.tar.gz
opensim-SC_OLD-0af17c94848724c0d56d4be12c01983b4793b8bf.tar.bz2
opensim-SC_OLD-0af17c94848724c0d56d4be12c01983b4793b8bf.tar.xz
llListRandomize() wasn't very random
Signed-off-by: Diva Canto <diva@metaverseink.com>
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r--OpenSim/Framework/Util.cs51
1 files changed, 51 insertions, 0 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index 56a90b1..2d0b280 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -3036,4 +3036,55 @@ namespace OpenSim.Framework
3036 } 3036 }
3037 } 3037 }
3038 } 3038 }
3039
3040 public class BetterRandom
3041 {
3042 private const int BufferSize = 1024; // must be a multiple of 4
3043 private byte[] RandomBuffer;
3044 private int BufferOffset;
3045 private RNGCryptoServiceProvider rng;
3046 public BetterRandom()
3047 {
3048 RandomBuffer = new byte[BufferSize];
3049 rng = new RNGCryptoServiceProvider();
3050 BufferOffset = RandomBuffer.Length;
3051 }
3052 private void FillBuffer()
3053 {
3054 rng.GetBytes(RandomBuffer);
3055 BufferOffset = 0;
3056 }
3057 public int Next()
3058 {
3059 if (BufferOffset >= RandomBuffer.Length)
3060 {
3061 FillBuffer();
3062 }
3063 int val = BitConverter.ToInt32(RandomBuffer, BufferOffset) & 0x7fffffff;
3064 BufferOffset += sizeof(int);
3065 return val;
3066 }
3067 public int Next(int maxValue)
3068 {
3069 return Next() % maxValue;
3070 }
3071 public int Next(int minValue, int maxValue)
3072 {
3073 if (maxValue < minValue)
3074 {
3075 throw new ArgumentOutOfRangeException("maxValue must be greater than or equal to minValue");
3076 }
3077 int range = maxValue - minValue;
3078 return minValue + Next(range);
3079 }
3080 public double NextDouble()
3081 {
3082 int val = Next();
3083 return (double)val / int.MaxValue;
3084 }
3085 public void GetBytes(byte[] buff)
3086 {
3087 rng.GetBytes(buff);
3088 }
3089 }
3039} 3090}