//-----------------------------------------------------------------------------
// RandomHelper.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
namespace RacingGame.Helpers
{
///
/// Random helper
///
public static class RandomHelper
{
///
/// Global random generator
///
public static Random globalRandomGenerator =
GenerateNewRandomGenerator();
///
/// Generate a new random generator with help of
/// WindowsHelper.GetPerformanceCounter.
/// Also used for all GetRandom methods here.
///
/// Random
public static Random GenerateNewRandomGenerator()
{
globalRandomGenerator =
new Random((int)DateTime.Now.Ticks);
//needs Interop: (int)WindowsHelper.GetPerformanceCounter());
return globalRandomGenerator;
}
///
/// Get random int
///
/// Maximum
/// Int
public static int GetRandomInt(int max)
{
return globalRandomGenerator.Next(max);
}
///
/// Get random float between min and max
///
/// Min
/// Max
/// Float
public static float GetRandomFloat(float min, float max)
{
return (float)globalRandomGenerator.NextDouble() * (max - min) + min;
}
///
/// Get random byte between min and max
///
/// Min
/// Max
/// Byte
public static byte GetRandomByte(byte min, byte max)
{
return (byte)(globalRandomGenerator.Next(min, max));
}
///
/// Get random Vector2
///
/// Minimum for each component
/// Maximum for each component
/// Vector2
public static Vector2 GetRandomVector2(float min, float max)
{
return new Vector2(
GetRandomFloat(min, max),
GetRandomFloat(min, max));
}
///
/// Get random Vector3
///
/// Minimum for each component
/// Maximum for each component
/// Vector3
public static Vector3 GetRandomVector3(float min, float max)
{
return new Vector3(
GetRandomFloat(min, max),
GetRandomFloat(min, max),
GetRandomFloat(min, max));
}
///
/// Get random color
///
/// Color
public static Color RandomColor
{
get
{
return new Color(new Vector3(
GetRandomFloat(0.25f, 1.0f),
GetRandomFloat(0.25f, 1.0f),
GetRandomFloat(0.25f, 1.0f)));
}
}
///
/// Get random normal Vector3
///
/// Vector3
public static Vector3 RandomNormalVector3
{
get
{
Vector3 randomNormalVector = new Vector3(
GetRandomFloat(-1.0f, 1.0f),
GetRandomFloat(-1.0f, 1.0f),
GetRandomFloat(-1.0f, 1.0f));
randomNormalVector.Normalize();
return randomNormalVector;
}
}
}
}