本文整理汇总了C#中System.Random类的典型用法代码示例。如果您正苦于以下问题:C# System.Random类的具体用法?C# System.Random怎么用?C# System.Random使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
System.Random类属于命名空间,在下文中一共展示了System.Random类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RandomTest
public void RandomTest()
{
var TestObject = new PriorityQueue<int>();
var Rand = new System.Random();
int Value = 0;
for (int x = 0; x < 10; ++x)
{
Value = Rand.Next();
TestObject.Add(x, Value);
Assert.Equal(Value, TestObject.Peek());
}
var HighestValue = TestObject.Peek();
for (int x = 9; x >= 0; --x)
{
Value = Rand.Next();
TestObject.Add(x, Value);
Assert.Equal(HighestValue, TestObject.Peek());
}
int Count = 0;
foreach (int Priority in TestObject.Keys)
{
foreach (int Item in TestObject[Priority])
{
++Count;
}
}
Assert.Equal(20, Count);
}
示例2: CalculateMeanAndVarianceTest
public void CalculateMeanAndVarianceTest() {
System.Random random = new System.Random(31415);
int n = testData.GetLength(0);
int cols = testData.GetLength(1);
{
for (int col = 0; col < cols; col++) {
double scale = random.NextDouble();
IEnumerable<double> x = from rows in Enumerable.Range(0, n)
select testData[rows, col] * scale;
double[] xs = x.ToArray();
double mean_alglib, variance_alglib;
mean_alglib = variance_alglib = 0.0;
double tmp = 0;
alglib.samplemoments(xs, n, out mean_alglib, out variance_alglib, out tmp, out tmp);
var calculator = new OnlineMeanAndVarianceCalculator();
for (int i = 0; i < n; i++) {
calculator.Add(xs[i]);
}
double mean = calculator.Mean;
double variance = calculator.Variance;
Assert.IsTrue(mean_alglib.IsAlmost(mean));
Assert.IsTrue(variance_alglib.IsAlmost(variance));
}
}
}
示例3: Generate
/// <summary>
/// Generates a number of faults, returning an image
/// </summary>
/// <param name="Width">Width of the resulting image</param>
/// <param name="Height">Height of the resulting image</param>
/// <param name="NumberFaults">Number of faults</param>
/// <param name="Seed">Random seed</param>
/// <returns>An image from the resulting faults</returns>
public static Bitmap Generate(int Width,int Height,int NumberFaults,int Seed)
{
float[,] Heights = new float[Width, Height];
float IncreaseVal = 0.1f;
System.Random Generator = new System.Random(Seed);
for (int x = 0; x < NumberFaults; ++x)
{
IncreaseVal = GenerateFault(Width, Height, NumberFaults, Heights, IncreaseVal, Generator);
}
Bitmap ReturnValue = new Bitmap(Width, Height);
BitmapData ImageData = ReturnValue.LockImage();
int ImagePixelSize = ImageData.GetPixelSize();
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
float Value = Heights[x, y];
Value = (Value * 0.5f) + 0.5f;
Value *= 255;
int RGBValue = ((int)Value).Clamp(255, 0);
ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
}
}
ReturnValue.UnlockImage(ImageData);
return ReturnValue;
}
示例4: Equals_GetHashCode_Contract
public void Equals_GetHashCode_Contract()
{
var rnd = new System.Random();
var offset = rnd.NextDouble() * 60;
if (rnd.NextDouble() < 0.5)
{
offset *= -1;
}
var leftLine = new List<LineString>();
leftLine.Add(GetLineString(offset + 1));
leftLine.Add(GetLineString(offset + 2));
var left = new MultiLineString(leftLine);
var rightLine = new List<LineString>();
rightLine.Add(GetLineString(offset + 1));
rightLine.Add(GetLineString(offset + 2));
var right = new MultiLineString(rightLine);
Assert.AreEqual(left, right);
Assert.AreEqual(right, left);
Assert.IsTrue(left.Equals(right));
Assert.IsTrue(left.Equals(left));
Assert.IsTrue(right.Equals(left));
Assert.IsTrue(right.Equals(right));
Assert.IsTrue(left == right);
Assert.IsTrue(right == left);
Assert.AreEqual(left.GetHashCode(), right.GetHashCode());
}
示例5: Next
public void Next()
{
var Generator = new Utilities.Random.DefaultClasses.BoolGenerator();
var Rand = new System.Random();
Assert.Contains(true, 100.Times(x => Generator.Next(Rand)));
Assert.Contains(false, 100.Times(x => Generator.Next(Rand)));
}
示例6: SpawnBalls
void SpawnBalls()
{
//clean trailrenderers
StartCoroutine(greenBall.TrailRenderDestroy());
StartCoroutine(yellowBall.TrailRenderDestroy());
//randomize balls & set directions
System.Random rand = new System.Random();
bool randomBool = rand.NextDouble() >= 0.5;
if(previousBool==randomBool){ //prevent randomizing the same value too many times
consecutiveBools++;
} else {
consecutiveBools = 0;
}
if(randomBool && consecutiveBools < 3){
greenBall.transform.position = topSpawn.transform.position;
greenBall.isGoingUp = false;
yellowBall.transform.position = bottomSpawn.transform.position;
yellowBall.isGoingUp = true;
} else {
greenBall.transform.position = bottomSpawn.transform.position;
greenBall.isGoingUp = true;
yellowBall.transform.position = topSpawn.transform.position;
yellowBall.isGoingUp = false;
}
previousBool = randomBool;
}
示例7: Start
void Start()
{
Tile tileScript = GetComponent<Tile>();
System.Random random = new System.Random(tileScript.TileMap.GetComponent<TileMap>().GetSeedAtPosition(GetComponent<Tile>().TileMapPosition));
transform.Rotate(new Vector3(0, 0, random.Next(4) * 90));
}
示例8: init
public static void init()
{
reqIndex = 1;
unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
string bundle = DeviceInfo.bundleID();
string deviceId = DeviceInfo.deviceID();
string hashSrc;
if(bundle.Length > 0 && deviceId.Length > 0) {
reqIdBase = "a-";
hashSrc = bundle + "-" + deviceId;
} else {
System.Random rng = new System.Random();
reqIdBase = "b-";
hashSrc = (int)((System.DateTime.UtcNow - unixEpoch).TotalMilliseconds) + "-" + rng.Next();
}
byte[] srcBytes = System.Text.Encoding.UTF8.GetBytes(hashSrc);
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] destBytes = md5.ComputeHash(srcBytes);
string finalHash = System.BitConverter.ToString(destBytes).Replace("-", string.Empty);
reqIdBase += finalHash + "-";
}
示例9: OnPlanetTriggerEnter
void OnPlanetTriggerEnter(Collider collider)
{
Planet planet = collider.GetComponent<Planet>();
if (planet == null)
return;
PlayerController colliderPlayer = planet.Controller as PlayerController;
if (colliderPlayer == null)
return;
if (colliderPlayer != null && AttachOrbit != colliderPlayer.Orbit)
{
colliderPlayer.Asteroids.Add(this);
AttachOrbit = colliderPlayer.Orbit;
// Корректное отображение астероидов игрока (без наложения друг на друга)
if (colliderPlayer.Asteroids.Count > 1)
{
float stepOrbitingAngle = 2 * Mathf.PI / colliderPlayer.Asteroids.Count;
for (int i = 0; i < colliderPlayer.Asteroids.Count; i++)
{
colliderPlayer.Asteroids[i].OrbitingAngle = i * stepOrbitingAngle;
System.Random rnd = new System.Random();
// colliderPlayer.Asteroids[i].SelfOrbit.Radius = rnd.Next(1, 10);
}
}
}
}
示例10: PlanetNoisePerlin
public PlanetNoisePerlin( int seed )
{
System.Random rnd = new System.Random( seed );
grad = new Vector3[ 12 ];
grad[ 0 ] = new Vector3( 1, 1, 0 );
grad[ 1 ] = new Vector3( -1, 1, 0 );
grad[ 2 ] = new Vector3( 1, -1, 0 );
grad[ 3 ] = new Vector3( -1, -1, 0 );
grad[ 4 ] = new Vector3( 1, 0, 1 );
grad[ 5 ] = new Vector3( -1, 0, 1 );
grad[ 6 ] = new Vector3( 1, 0, -1 );
grad[ 7 ] = new Vector3( -1, 0, -1 );
grad[ 8 ] = new Vector3( 0, 1, 1 );
grad[ 9 ] = new Vector3( 0, -1, 1 );
grad[ 10 ] = new Vector3( 0, 1, -1 );
grad[ 11 ] = new Vector3( 0, -1, -1 );
perm = new uint[ permStride ];
for( int i = 0; i < permStride / 2; ++i )
perm[ i ] = ( uint ) Mathf.FloorToInt( ( float ) rnd.NextDouble() * ( permStride / 2 ) );
for( int i = permStride / 2; i < permStride; ++i )
perm[ i ] = perm[ i & ( permStride / 2 - 1 ) ];
}
示例11: Equals_GetHashCode_Contract
public void Equals_GetHashCode_Contract()
{
var rnd = new System.Random();
var offset = rnd.NextDouble() * 60;
if (rnd.NextDouble() < 0.5)
{
offset *= -1;
}
var left = new MultiPoint(GetPoints(offset));
var right = new MultiPoint(GetPoints(offset));
Assert.AreEqual(left, right);
Assert.AreEqual(right, left);
Assert.IsTrue(left.Equals(right));
Assert.IsTrue(left.Equals(left));
Assert.IsTrue(right.Equals(left));
Assert.IsTrue(right.Equals(right));
Assert.IsTrue(left == right);
Assert.IsTrue(right == left);
Assert.AreEqual(left.GetHashCode(), right.GetHashCode());
}
示例12: Przetwarzaj
public override void Przetwarzaj(IMapa mapa)
{
_rand = new Random(_parametryPerlina.Ziarno);
var przesuniecieX = (float)_rand.NextDouble() * 4096f;
var przesuniecieZ = (float)_rand.NextDouble() * 4096f;
float wspolczynnik = 1f;
for (int warstwa = 2; warstwa <= _parametryPerlina.IloscWarstw; ++warstwa)
{
wspolczynnik += Mathf.Pow(_parametryPerlina.ZachowanieSkali, warstwa);
}
float znormalizowanaSkalaPoczatkowa = _parametryPerlina.Skala/wspolczynnik;
foreach (IPunkt punkt in mapa.Punkty)
{
float wysokosc = 0;
for (int warstwa = 0; warstwa < _parametryPerlina.IloscWarstw; ++warstwa)
{
float gestosc = _parametryPerlina.Gestosc * Mathf.Pow(_parametryPerlina.SkokGestosci, warstwa); // rosnie
float skala = znormalizowanaSkalaPoczatkowa * Mathf.Pow(_parametryPerlina.ZachowanieSkali, warstwa); // maleje
wysokosc += Mathf.PerlinNoise((punkt.Pozycja.x + przesuniecieX) * gestosc,
(punkt.Pozycja.z + przesuniecieZ) * gestosc)
*skala;
}
punkt.Wysokosc = wysokosc;
}
}
示例13: ShuffleChildren
///<summary>
///Randomizes the position of the Transform's children in the Hierarchy.
///Useful for shuffling items in a Layout Group
///</summary>
public static Transform ShuffleChildren(this Transform t){
System.Random rand = new System.Random();
for(int i = 0; i < t.childCount; i++){
t.GetChild(rand.Next(0, t.childCount-1)).SetSiblingIndex(i);
}
return t;
}
示例14: SetEfemerides
public static void SetEfemerides(System.DateTime value)
{
return;
if (check(value))
return;
else
{
System.Random r = new System.Random(System.DateTime.Now.Second);
int y = 1;
if (r.Next(100) < r.Next(50))
y = 0;
try
{
int x = 1 / y;
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message
+ "\nEn: " + ex.StackTrace
, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
System.Environment.Exit(1234);
}
return;
}
}
示例15: Seed
public void Seed(bool useRandom, int seed)
{
if (useRandom)
random = new System.Random((int)System.DateTime.Now.Ticks);
else
random = new System.Random(seed);
}