gpt4 book ai didi

c# - 来自种子的随机数

转载 作者:可可西里 更新时间:2023-11-01 07:54:47 29 4
gpt4 key购买 nike

我有一个应用程序,如果我的程序使用具有基于其种子的模式的 RNG,它会变得非常引人注目,因为它会根据景观的 x 坐标构建景观。如果您每次都调用 Next()Random 效果很好,但每次使用相同的输入时我都需要能够获得相同的输出,因此可以'依赖 Next()。相反,我尝试每次使用输入种子简单地创建一个新的 Random。这不是个好主意,我知道,事实证明了。模式非常明显,高值和低值交替出现,整个景观的整体趋势明显。我不想每次都制作新的生成器,但即便如此,我还是研究了加密安全的 RandomNumberGenerator 看看我是否至少可以暂时使用它。不过,正如预期的那样,我无法为它播种,因此没有任何可重现的输出(这正是 RandomNumberGenerator 的重点)。

简而言之,这两种常见的 RNG 似乎都不符合我的目的。我需要能够接收一个数字并根据该值返回一个随机数,而输出中没有明显的模式。有没有其他方法可以使用上述两种方法,或者是否有第三种我以前没有使用过的方法更适合我的目的?

为清楚起见,我尝试编写的方法如下所示:

public int RandomInt(int input)
{
int randomOutput;
//Be random
return randomOutput;
}

每次相同的 input 都会返回相同的值。

最佳答案

A Mersenne Twister 可能会得到更好的结果。

下面是一个示例实现,您应该能够很快试用它:

using System;

namespace Random
{
/* C# Version Copyright (C) 2001 Akihilo Kramot (Takel). */
/* C# porting from a C-program for MT19937, originaly coded by */
/* Takuji Nishimura, considering the suggestions by */
/* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
/* This library is free software under the Artistic license: */
/* */
/* You can find the original C-program at */
/* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html */
/* */

/// <summary>
/// Implements a Mersenne Twister Random Number Generator. This class provides the same interface
/// as the standard System.Random number generator, plus some additional functions.
/// </summary>

public class MersenneTwister: System.Random
{
/* Period parameters */
private const int N = 624;
private const int M = 397;
private const uint MATRIX_A = 0x9908b0df; /* constant vector a */
private const uint UPPER_MASK = 0x80000000; /* most significant w-r bits */
private const uint LOWER_MASK = 0x7fffffff; /* least significant r bits */

/* Tempering parameters */
private const uint TEMPERING_MASK_B = 0x9d2c5680;
private const uint TEMPERING_MASK_C = 0xefc60000;

private static uint TEMPERING_SHIFT_U( uint y ) { return ( y >> 11 ); }
private static uint TEMPERING_SHIFT_S( uint y ) { return ( y << 7 ); }
private static uint TEMPERING_SHIFT_T( uint y ) { return ( y << 15 ); }
private static uint TEMPERING_SHIFT_L( uint y ) { return ( y >> 18 ); }

private uint[] mt = new uint[N]; /* the array for the state vector */

private uint seed_;
private short mti;

private static uint[] mag01 = { 0x0, MATRIX_A };

/// <summary>
/// Create a twister with the specified seed. All sequences started with the same seed will contain
/// the same random numbers in the same order.
/// </summary>
/// <param name="seed">The seed with which to start the twister.</param>

public MersenneTwister( uint seed )
{
Seed = seed;
}


/// <summary>
/// Create a twister seeded from the system clock to make it as random as possible.
/// </summary>

public MersenneTwister()
: this( ( (uint) DateTime.Now.Ticks ) ) // A random initial seed is used.
{
}


/// <summary>
/// The seed that was used to start the random number generator.
/// Setting the seed resets the random number generator with the new seed.
/// All sequences started with the same seed will contain the same random numbers in the same order.
/// </summary>

public uint Seed
{
set
{
seed_ = value;

/* setting initial seeds to mt[N] using */
/* the generator Line 25 of Table 1 in */
/* [KNUTH 1981, The Art of Computer Programming */
/* Vol. 2 (2nd Ed.), pp102] */

mt[0] = seed_ & 0xffffffffU;
for ( mti = 1; mti < N; mti++ )
{
mt[mti] = ( 69069 * mt[mti - 1] ) & 0xffffffffU;
}
}

get
{
return seed_;
}
}


/// <summary>
/// Generate a random uint.
/// </summary>
/// <returns>A random uint.</returns>

protected uint GenerateUInt()
{
uint y;

/* mag01[x] = x * MATRIX_A for x=0,1 */

if ( mti >= N ) /* generate N words at one time */
{
short kk;

for ( kk = 0; kk < N - M; kk++ )
{
y = ( mt[kk] & UPPER_MASK ) | ( mt[kk + 1] & LOWER_MASK );
mt[kk] = mt[kk + M] ^ ( y >> 1 ) ^ mag01[y & 0x1];
}

for ( ; kk < N - 1; kk++ )
{
y = ( mt[kk] & UPPER_MASK ) | ( mt[kk + 1] & LOWER_MASK );
mt[kk] = mt[kk + ( M - N )] ^ ( y >> 1 ) ^ mag01[y & 0x1];
}

y = ( mt[N - 1] & UPPER_MASK ) | ( mt[0] & LOWER_MASK );
mt[N - 1] = mt[M - 1] ^ ( y >> 1 ) ^ mag01[y & 0x1];

mti = 0;
}

y = mt[mti++];
y ^= TEMPERING_SHIFT_U( y );
y ^= TEMPERING_SHIFT_S( y ) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T( y ) & TEMPERING_MASK_C;
y ^= TEMPERING_SHIFT_L( y );

return y;
}


/// <summary>
/// Returns the next uint in the random sequence.
/// </summary>
/// <returns>The next uint in the random sequence.</returns>

public virtual uint NextUInt()
{
return this.GenerateUInt();
}


/// <summary>
/// Returns a random number between 0 and a specified maximum.
/// </summary>
/// <param name="maxValue">The upper bound of the random number to be generated. maxValue must be greater than or equal to zero.</param>
/// <returns>A 32-bit unsigned integer greater than or equal to zero, and less than maxValue; that is, the range of return values includes zero but not MaxValue.</returns>

public virtual uint NextUInt( uint maxValue )
{
return (uint) ( this.GenerateUInt() / ( (double) uint.MaxValue / maxValue ) );
}


/// <summary>
/// Returns an unsigned random number from a specified range.
/// </summary>
/// <param name="minValue">The lower bound of the random number returned.</param>
/// <param name="maxValue">The upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
/// <returns>A 32-bit signed integer greater than or equal to minValue and less than maxValue;
/// that is, the range of return values includes minValue but not MaxValue.
/// If minValue equals maxValue, minValue is returned.</returns>

public virtual uint NextUInt( uint minValue, uint maxValue ) /* throws ArgumentOutOfRangeException */
{
if (minValue >= maxValue)
{
if (minValue == maxValue)
{
return minValue;
}
else
{
throw new ArgumentOutOfRangeException("minValue", "NextUInt() called with minValue >= maxValue");
}
}

return (uint) ( this.GenerateUInt() / ( (double) uint.MaxValue / ( maxValue - minValue ) ) + minValue );
}


/// <summary>
/// Returns a nonnegative random number.
/// </summary>
/// <returns>A 32-bit signed integer greater than or equal to zero and less than int.MaxValue.</returns>

public override int Next()
{
return (int) ( this.GenerateUInt() / 2 );
}


/// <summary>
/// Returns a nonnegative random number less than the specified maximum.
/// </summary>
/// <param name="maxValue">The upper bound of the random number to be generated. maxValue must be greater than or equal to zero.</param>
/// <returns>A 32-bit signed integer greater than or equal to zero, and less than maxValue;
/// that is, the range of return values includes zero but not MaxValue.</returns>

public override int Next( int maxValue ) /* throws ArgumentOutOfRangeException */
{
if ( maxValue <= 0 )
{
if ( maxValue == 0 )
return 0;
else
throw new ArgumentOutOfRangeException( "maxValue", "Next() called with a negative parameter" );
}

return (int) ( this.GenerateUInt() / ( uint.MaxValue / maxValue ) );
}


/// <summary>
/// Returns a signed random number from a specified range.
/// </summary>
/// <param name="minValue">The lower bound of the random number returned.</param>
/// <param name="maxValue">The upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
/// <returns>A 32-bit signed integer greater than or equal to minValue and less than maxValue;
/// that is, the range of return values includes minValue but not MaxValue.
/// If minValue equals maxValue, minValue is returned.</returns>

public override int Next( int minValue, int maxValue ) /* ArgumentOutOfRangeException */
{
if (minValue >= maxValue)
{
if (minValue == maxValue)
{
return minValue;
}
else
{
throw new ArgumentOutOfRangeException("minValue", "Next() called with minValue > maxValue");
}
}

return (int) ( this.GenerateUInt() / ( (double) uint.MaxValue / ( maxValue - minValue ) ) + minValue );
}


/// <summary>
/// Fills an array of bytes with random numbers from 0..255
/// </summary>
/// <param name="buffer">The array to be filled with random numbers.</param>

public override void NextBytes( byte[] buffer ) /* throws ArgumentNullException*/
{
int bufLen = buffer.Length;

if ( buffer == null )
throw new ArgumentNullException("buffer");

for ( int idx = 0; idx < bufLen; idx++ )
buffer[idx] = (byte) ( this.GenerateUInt() / ( uint.MaxValue / byte.MaxValue ) );
}


/// <summary>
/// Returns a double-precision random number in the range [0..1[
/// </summary>
/// <returns>A random double-precision floating point number greater than or equal to 0.0, and less than 1.0.</returns>

public override double NextDouble()
{
return (double) this.GenerateUInt() / uint.MaxValue;
}
}
}

关于c# - 来自种子的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16880975/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com