| 1 | /* |
|---|
| 2 | * $Id: PRNG.cs 1802 2010-02-10 09:08:32Z lowjoel $ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Linq; |
|---|
| 25 | using System.Text; |
|---|
| 26 | |
|---|
| 27 | namespace Eraser.Plugins.ExtensionPoints |
|---|
| 28 | { |
|---|
| 29 | /// <summary> |
|---|
| 30 | /// An interface class for all pseudorandom number generators used for the |
|---|
| 31 | /// random data erase passes. |
|---|
| 32 | /// </summary> |
|---|
| 33 | public abstract class IPrng : IRegisterable |
|---|
| 34 | { |
|---|
| 35 | public override string ToString() |
|---|
| 36 | { |
|---|
| 37 | return Name; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /// <summary> |
|---|
| 41 | /// The name of this erase pass, used for display in the UI |
|---|
| 42 | /// </summary> |
|---|
| 43 | public abstract string Name |
|---|
| 44 | { |
|---|
| 45 | get; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /// <summary> |
|---|
| 49 | /// The GUID for this PRNG. |
|---|
| 50 | /// </summary> |
|---|
| 51 | public abstract Guid Guid |
|---|
| 52 | { |
|---|
| 53 | get; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /// <summary> |
|---|
| 57 | /// Reseeds the PRNG. This can be called by inherited classes, but its most |
|---|
| 58 | /// important function is to provide new seeds regularly. The PRNGManager |
|---|
| 59 | /// will call this function once in a whle to maintain the quality of |
|---|
| 60 | /// generated numbers. |
|---|
| 61 | /// </summary> |
|---|
| 62 | /// <param name="seed">An arbitrary length of information that will be |
|---|
| 63 | /// used to reseed the PRNG</param> |
|---|
| 64 | protected internal abstract void Reseed(byte[] seed); |
|---|
| 65 | |
|---|
| 66 | #region Random members |
|---|
| 67 | /// <summary> |
|---|
| 68 | /// Returns a nonnegative random number less than the specified maximum. |
|---|
| 69 | /// </summary> |
|---|
| 70 | /// <param name="maxValue">The exclusive upper bound of the random number |
|---|
| 71 | /// to be generated. maxValue must be greater than or equal to zero.</param> |
|---|
| 72 | /// <returns>A 32-bit signed integer greater than or equal to zero, and |
|---|
| 73 | /// less than maxValue; that is, the range of return values ordinarily |
|---|
| 74 | /// includes zero but not maxValue. However, if maxValue equals zero, |
|---|
| 75 | /// maxValue is returned.</returns> |
|---|
| 76 | public int Next(int maxValue) |
|---|
| 77 | { |
|---|
| 78 | if (maxValue == 0) |
|---|
| 79 | return 0; |
|---|
| 80 | return Next() % maxValue; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /// <summary> |
|---|
| 84 | /// Returns a random number within a specified range. |
|---|
| 85 | /// </summary> |
|---|
| 86 | /// <param name="minValue">The inclusive lower bound of the random number |
|---|
| 87 | /// returned.</param> |
|---|
| 88 | /// <param name="maxValue">The exclusive upper bound of the random number |
|---|
| 89 | /// returned. maxValue must be greater than or equal to minValue.</param> |
|---|
| 90 | /// <returns>A 32-bit signed integer greater than or equal to minValue and |
|---|
| 91 | /// less than maxValue; that is, the range of return values includes minValue |
|---|
| 92 | /// but not maxValue. If minValue equals maxValue, minValue is returned.</returns> |
|---|
| 93 | public int Next(int minValue, int maxValue) |
|---|
| 94 | { |
|---|
| 95 | if (minValue > maxValue) |
|---|
| 96 | throw new ArgumentOutOfRangeException("minValue", minValue, |
|---|
| 97 | "minValue is greater than maxValue"); |
|---|
| 98 | else if (minValue == maxValue) |
|---|
| 99 | return minValue; |
|---|
| 100 | return (Next() % (maxValue - minValue)) + minValue; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /// <summary> |
|---|
| 104 | /// Returns a nonnegative random number. |
|---|
| 105 | /// </summary> |
|---|
| 106 | /// <returns>A 32-bit signed integer greater than or equal to zero and less |
|---|
| 107 | /// than System.Int32.MaxValue.</returns> |
|---|
| 108 | public int Next() |
|---|
| 109 | { |
|---|
| 110 | //Get the random-valued bytes to fill the int. |
|---|
| 111 | byte[] rand = new byte[sizeof(int)]; |
|---|
| 112 | NextBytes(rand); |
|---|
| 113 | |
|---|
| 114 | //Then return the integral representation of the buffer. |
|---|
| 115 | return Math.Abs(BitConverter.ToInt32(rand, 0)); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /// <summary> |
|---|
| 119 | /// Fills the elements of a specified array of bytes with random numbers. |
|---|
| 120 | /// </summary> |
|---|
| 121 | /// <param name="buffer">An array of bytes to contain random numbers.</param> |
|---|
| 122 | public abstract void NextBytes(byte[] buffer); |
|---|
| 123 | #endregion |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | } |
|---|