/// <summary> /// Generates a new random seed of SeedLength values for use with seeding the generator, /// but does not modify the state of the generator itself. It is called by Randomize(). /// <para>The seed is generated by the underlying OS via the RNGCryptoServiceProvider /// class. It may be overridden to generate seeds according to custom requirements.</para> /// </summary> public virtual TInteg[] GenerateSeed() { TInteg[] rslt = null; byte[] bytes = new byte[SeedLength * TypeSize]; if (bytes.Length > 0) { RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider(); crypto.GetBytes(bytes); rslt = CleromUtil.ConvertBytesToArray <TInteg>(bytes); } return(rslt); }
/// <summary> /// Sets the generator to a new state, defined by a single 64-bit integer seed value. /// <para>This overloaded variant of SetSeed() provides a convenient means to seed /// any generator with a single integer value.</para> /// <para>Its behavior is as follows:</para> /// <para>If SeedLength is 1, the seed value will be cast to the corresponding TInteg /// type, copied to an array of length 1, and passed directly to SetSeed(TInteg[]).</para> /// <para>If SeedLength is larger than 1, the integer seed will be "expanded" to an array /// of SeedLength values, which will then be passed to SetSeed(TInteg[]).</para> /// <para>The number of possible seed values using this method may, therefore, be /// significantly less than that supported by the SetSeed(TInteg[]) array variant.</para> /// <para>Seeding with this method or, indeed, with any single integer value, should /// not be considered suitable for cryptographic use.</para> /// <para>Furthermore, seed expansions are to be considered implementation dependent, /// in the sense that the implementation may potentially change with some future update /// to the library.</para> /// <para>Currently, expansions are performed using the SPLITMIX64 generator which, /// itself, has a 64-bit seed type. If you require future-proof repeatability, /// override this method with your own.</para> /// </summary> /// <param name="seed">Integer seed value</param> public virtual void SetSeed(ulong seed) { if (SeedLength == 1) { var arr = new TInteg[1]; arr[0] = (TInteg)(dynamic)seed; SetSeed(arr); } else { SplitMix64 temp = new SplitMix64(); temp.SetSeed(seed); byte[] bs = temp.GetBytes(TypeSize * SeedLength); SetSeed(CleromUtil.ConvertBytesToArray <TInteg>(bs)); } }