本文整理汇总了C#中Random.NextDouble方法的典型用法代码示例。如果您正苦于以下问题:C# Random.NextDouble方法的具体用法?C# Random.NextDouble怎么用?C# Random.NextDouble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Random
的用法示例。
在下文中一共展示了Random.NextDouble方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Draw
public override void Draw(SpriteBatch sb, Projectile p) {
Vector2 pc = p.position+new Vector2(p.width/2f,p.height/2f);
if (fired) {
sb.End();
sb.Begin(SpriteSortMode.Immediate,BlendState.Additive);
foreach (Spark spark in sparks) spark.Draw(sb,p,this);
for (int i = 0; i < sparks.Count; i++) if (sparks[i].dead) sparks.RemoveAt(i--);
sb.End();
sb.Begin();
if (sparks.Count == 0) p.Kill();
} else {
fired = true;
int r, g, b;
Color c;
float degrees;
byte[] bytes = BitConverter.GetBytes(seed);
Random rnd = new Random();
degrees = (float)(rnd.NextDouble()*360d);
HsvToRgb(bytes[0]*360f/255f,bytes[1]/255f,1f,out r,out g,out b);
c = new Color(r,g,b);
for (int i = 0; i < 20; i++) sparks.Add(new MySpark(pc,Util.Vector((float)(1.5f+rnd.NextDouble()*.75f),360f/20f*i+degrees),c,80+rnd.Next(40)));
degrees = (float)(rnd.NextDouble()*360d);
HsvToRgb(bytes[2]*360f/255f,bytes[3]/255f,1f,out r,out g,out b);
c = new Color(r,g,b);
for (int i = 0; i < 40; i++) sparks.Add(new MySpark(pc,Util.Vector((float)(3f+rnd.NextDouble()*1.5f),360f/40f*i+degrees),c,80+rnd.Next(40)));
}
}
示例2: PosTest2
public bool PosTest2()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest2: Verify Double value not equals with Equals(Double).");
try
{
Random random = new Random(-55);
Double d1,d2;
do
{
d1 = random.NextDouble();
d2 = random.NextDouble();
}
while (d1 == d2);
if (d1.Equals(d2))
{
TestLibrary.TestFramework.LogError("002.1", "Method Double.Equals(Double) Err.");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002.2", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
return retVal;
}
示例3: Main
static void Main(string[] args)
{
int dartsPerProcessor = 10000;
using (new MPI.Environment(ref args))
{
if (args.Length > 0)
dartsPerProcessor = Convert.ToInt32(args[0]);
Intracommunicator world = Communicator.world;
Random random = new Random(5 * world.Rank);
int dartsInCircle = 0;
for (int i = 0; i < dartsPerProcessor; ++i)
{
double x = (random.NextDouble() - 0.5) * 2;
double y = (random.NextDouble() - 0.5) * 2;
if (x * x + y * y <= 1.0)
++dartsInCircle;
}
if (world.Rank == 0)
{
int totalDartsInCircle = world.Reduce<int>(dartsInCircle, Operation<int>.Add, 0);
System.Console.WriteLine("Pi is approximately {0:F15}.",
4*(double)totalDartsInCircle/(world.Size*(double)dartsPerProcessor));
}
else
{
world.Reduce<int>(dartsInCircle, Operation<int>.Add, 0);
}
}
}
示例4: PosTest1
public bool PosTest1()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest1: Ensure the return value of CompareTo(System.Double) is less than zero when the instance is less than System.Double.");
try
{
Random random = new Random(-55);
Double d1, d2;
do
{
d1 = random.NextDouble();
d2 = random.NextDouble();
}
while (d1 >= d2);
if (d1.CompareTo(d2) >= 0)
{
TestLibrary.TestFramework.LogError("001.1", "The return value of CompareTo(System.Double) is not less than zero when the instance is less than System.Double!");
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
return retVal;
}
示例5: SparseSquareMatrixAgreement
public void SparseSquareMatrixAgreement()
{
int d = 6;
SparseSquareMatrix A = new SparseSquareMatrix(d);
SquareMatrix B = new SquareMatrix(d);
Random rng = new Random(1);
for (int i = 0; i < 2 * d; i++) {
int r = (int) Math.Floor(rng.NextDouble() * d);
int c = (int) Math.Floor(rng.NextDouble() * d);
A[r, c] = 2.0 * rng.NextDouble() - 1.0;
B[r, c] = A[r, c];
}
RowVector u = new RowVector(d);
ColumnVector v = new ColumnVector(d);
for (int i = 0; i < d; i++) {
u[i] = 2.0 * rng.NextDouble() - 1.0;
v[i] = 2.0 * rng.NextDouble() - 1.0;
}
RowVector uA = u * A;
RowVector uB = u * B;
Assert.IsTrue(TestUtilities.IsNearlyEqual(uA, uB));
ColumnVector Av = A * v;
ColumnVector Bv = B * v;
Assert.IsTrue(TestUtilities.IsNearlyEqual(Av, Bv));
}
示例6: Operation
public void Operation(Operations operation)
{
Random rand = new Random(84329);
Vector2 v1 = new Vector2(Convert.ToSingle(rand.NextDouble()), Convert.ToSingle(rand.NextDouble()));
Vector2 v2 = new Vector2(Convert.ToSingle(rand.NextDouble()), Convert.ToSingle(rand.NextDouble()));
foreach (var iteration in Benchmark.Iterations)
using (iteration.StartMeasurement())
ExecuteTest(operation, 1000000, v1, v2);
}
示例7: Particlecoll
public Particlecoll(Vector2f start, float speed)
{
r = new Random();
lifetime = r.NextDouble() * 3000 + 1000f;
this.speed = speed;
tex = new Texture("Staub.bmp");
spr = new Sprite(tex);
isAlive = true;
dir = new Vector2f((float)r.NextDouble() * 2 - 1, (float)r.NextDouble() * 2 - 1);
}
示例8: RandomXY
public static Vector3 RandomXY()
{
Random random = new Random(Environment.TickCount);
Vector3 vector3 = new Vector3();
vector3.X = (float)(random.NextDouble() - 0.5);
vector3.Y = (float)(random.NextDouble() - 0.5);
vector3.Z = 0.0f;
vector3.Normalize();
return vector3;
}
示例9: Main
public static void Main (string[] args)
{
Random random = new Random ();
while (true) {
WeightedPick<int> p = new WeightedPick<int> ();
p.Add ((float)random.NextDouble()*.00004f, 1, .4f, .004f);
p.Add (-(float)random.NextDouble()*.00004f, 2, .4f, .004f);
Console.WriteLine ("picked:"+p.RandomPick ().Item);
}
}
示例10: CreateRandomTridiagonalMatrix
public TridiagonalMatrix CreateRandomTridiagonalMatrix(int n, Random rng)
{
TridiagonalMatrix T = new TridiagonalMatrix(n);
for (int i = 0; i < (n-1); i++) {
T[i, i] = 2.0 * rng.NextDouble() - 1.0;
T[i + 1, i] = 2.0 * rng.NextDouble() - 1.0;
T[i, i + 1] = 2.0 * rng.NextDouble() - 1.0;
}
T[n - 1, n - 1] = 2.0 * rng.NextDouble() - 1.0;
return (T);
}
示例11: CreateRandomSparseSquareMatrix
public SparseSquareMatrix CreateRandomSparseSquareMatrix(int dim, int fill, Random rng)
{
SparseSquareMatrix S = new SparseSquareMatrix(dim);
for (int i = 0; i < fill; i++) {
int r = (int)Math.Floor(rng.NextDouble() * dim);
int c = (int)Math.Floor(rng.NextDouble() * dim);
S[r, c] = 2.0 * rng.NextDouble() - 1.0;
}
return (S);
}
示例12: Main
static void Main()
{
//From the book - task 4
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Random randomNumber = new Random();
int randomInteger = randomNumber.Next(0, 99999);
double randomPositive = randomNumber.NextDouble();
double randomNegative = -(randomNumber.NextDouble());
double randomPositiveDouble = randomPositive * randomInteger;
double randomNegativeDouble = randomNegative * randomInteger;
Console.WriteLine("{0,-10:X}{1,-10:F2}{2,-10:F2}", randomInteger, randomPositiveDouble, randomNegativeDouble);
}
示例13: Main
public static void Main()
{
Random r = new Random();
Console.WriteLine(r.NextDouble());
Console.WriteLine(r.NextDouble());
Console.WriteLine(r.Next());
Console.WriteLine(r.Next());
Console.WriteLine(r.Next(30));
Console.WriteLine(r.Next(30));
Console.WriteLine(r.Next(100, 125));
Console.WriteLine(r.Next(100, 125));
}
示例14: Main
public static void Main() {
modshogun.init_shogun_with_defaults();
int num = 1000;
double dist = 1.0;
double width = 2.1;
double C = 1.0;
Random RandomNumber = new Random();
double[,] traindata_real = new double[2, num * 2];
for (int i = 0; i < num; i ++) {
traindata_real[0, i] = RandomNumber.NextDouble() - dist;
traindata_real[0, i + num] = RandomNumber.NextDouble() + dist;
traindata_real[1, i] = RandomNumber.NextDouble() - dist;
traindata_real[1, i + num] = RandomNumber.NextDouble() + dist;
}
double[,] testdata_real = new double[2, num * 2];
for (int i = 0; i < num; i ++) {
testdata_real[0, i] = RandomNumber.NextDouble() - dist;
testdata_real[0, i + num] = RandomNumber.NextDouble() + dist;
testdata_real[1, i] = RandomNumber.NextDouble() - dist;
testdata_real[1, i + num] = RandomNumber.NextDouble() + dist;
}
double[] trainlab = new double[num * 2];
for (int i = 0; i < num; i ++) {
trainlab[i] = -1;
trainlab[i + num] = 1;
}
double[] testlab = new double[num * 2];
for (int i = 0; i < num; i ++) {
testlab[i] = -1;
testlab[i + num] = 1;
}
RealFeatures feats_train = new RealFeatures(traindata_real);
RealFeatures feats_test = new RealFeatures(testdata_real);
GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);
BinaryLabels labels = new BinaryLabels(trainlab);
LibSVM svm = new LibSVM(C, kernel, labels);
svm.train();
double[] result = LabelsFactory.to_binary(svm.apply(feats_test)).get_labels();
int err_num = 0;
for (int i = 0; i < num; i++) {
if (result[i] > 0) {
err_num += 1;
}
if (result[i+num] < 0) {
err_num += 1;
}
}
double testerr=err_num/(2*num);
Console.WriteLine(testerr);
}
示例15: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
chart.Title = new Title("Pie Chart");
OpenFlashChart.Pie pie = new OpenFlashChart.Pie();
Random random = new Random();
List<PieValue> values = new List<PieValue>();
List<string> labels = new List<string>();
for (int i = 0; i < 12; i++)
{
values.Add(new PieValue(random.NextDouble(),"Pie"+i));
labels.Add(i.ToString());
}
values.Add(0.2);
pie.Values = values;
// pie.Alpha = 50;
//pie.Colour = "#fff";
pie.Colours = new string[]{"#04f","#1ff","#6ef","#f30"};
chart.AddElement(pie);
string s = chart.ToPrettyString();
Response.Clear();
Response.CacheControl = "no-cache";
Response.Write(s);
Response.End();
}