public void NextFloat10M() { for (int i = 0; i < __loops; i++) { _rng.NextFloat(); } }
public Color[] NextFrame() { Parallel.For(0, renderConfig.Height, y => { Parallel.For(0, renderConfig.Width, x => { // chapter 1 outputs the image to the image file, we're flipping the // row pointer so that we start from the other end in order to get same visual result. var index = (renderConfig.Height - 1 - y) * renderConfig.Width + x; if (renderConfig.Antialiasing) { Vector3 color = new Vector3(); for (int i = 0; i < renderConfig.AASamples; i++) { float u = (x + XorShiftRandom.NextFloat()) / renderConfig.Width; float v = (y + XorShiftRandom.NextFloat()) / renderConfig.Height; color += PixelColor(camera.GetRay(u, v), World, 0); } color /= renderConfig.AASamples; color = new Vector3(MathUtil.FastSqrt(color.X), MathUtil.FastSqrt(color.Y), MathUtil.FastSqrt(color.Z)); color *= 255.99f; frameBuffer[index].R = (byte)color.X; frameBuffer[index].G = (byte)color.Y; frameBuffer[index].B = (byte)color.Z; frameBuffer[index].A = 255; } else { float u = (float)x / renderConfig.Width; float v = (float)y / renderConfig.Height; var color = PixelColor(camera.GetRay(u, v), World, 0); color = new Vector3(MathUtil.FastSqrt(color.X), MathUtil.FastSqrt(color.Y), MathUtil.FastSqrt(color.Z)); color *= 255.99f; frameBuffer[index].R = (byte)color.X; frameBuffer[index].G = (byte)color.Y; frameBuffer[index].B = (byte)color.Z; frameBuffer[index].A = 255; } }); }); return(frameBuffer); }
public void NextFloat() { int sampleCount = 10000000; XorShiftRandom rng = new XorShiftRandom(); double[] sampleArr = new double[sampleCount]; for (int i = 0; i < sampleCount; i++) { sampleArr[i] = rng.NextFloat(); } UniformDistributionTest(sampleArr, 0.0, 1.0); }
private List <Hitable> RandomScene() { var result = new List <Hitable>(); result.Add(new Sphere(new Vector3(0f, -1000f, 0f), 1000f, new Lambertian(new Vector3(0.5f, 0.5f, 0.5f)))); result.Add(new Sphere(new Vector3(0f, 1f, 0f), 1f, new Dielectric(1.5f))); result.Add(new Sphere(new Vector3(-4f, 1f, 0f), 1f, new Lambertian(new Vector3(0.4f, 0.2f, 0.1f)))); result.Add(new Sphere(new Vector3(4f, 1f, 0f), 1f, new Metal(new Vector3(0.7f, 0.6f, 0.5f), 0.0f))); for (int a = -11; a < 11; a++) { for (int b = -11; b < 11; b++) { float material = XorShiftRandom.NextFloat(); Vector3 center = new Vector3(a + 0.9f * XorShiftRandom.NextFloat(), 0.2f, b + 0.9f * XorShiftRandom.NextFloat()); if ((center - new Vector3(4f, 0f, 0.2f)).Length() > 0.9f) { if (material < 0.8f) { result.Add(new Sphere(center, 0.2f, new Lambertian(new Vector3(XorShiftRandom.NextFloat() * XorShiftRandom.NextFloat(), XorShiftRandom.NextFloat() * XorShiftRandom.NextFloat(), XorShiftRandom.NextFloat() * XorShiftRandom.NextFloat())))); } else if (material < 0.95f) { result.Add(new Sphere(center, 0.2f, new Metal(new Vector3(0.5f * (1 + XorShiftRandom.NextFloat()), 0.5f * (1 + XorShiftRandom.NextFloat()), 0.5f * (1 + XorShiftRandom.NextFloat())), 0.5f * XorShiftRandom.NextFloat()))); } else { result.Add(new Sphere(center, 0.2f, new Dielectric(1.5f))); } } } } return(result); }
/// <summary> /// Add objects to the Box2d world. /// </summary> protected override void PopulateWorld() { // ==== Define the ground body ==== BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(_trackLengthHalf, -1f); // Call the body factory which creates the ground box shape. // The body is also added to the world. Body groundBody = _world.CreateBody(groundBodyDef); // Define the ground box shape. PolygonDef groundShapeDef = new PolygonDef(); // The extents are the half-widths of the box. groundShapeDef.SetAsBox(_trackLengthHalf + 1f, 1f); groundShapeDef.Friction = _simParams._defaultFriction; groundShapeDef.Restitution = _simParams._defaultRestitution; groundShapeDef.Filter.CategoryBits = 0x3; // Add the ground shape to the ground body. groundBody.CreateShape(groundShapeDef); // Add some small mounds/bumps to the ground. for (float x = -1f; x < 40f; x += 0.4f + ((_rng.NextFloat() - 0.5f) * 0.15f)) { WalkerWorldUtils.CreateMound(_world, x, 0f, _simParams._defaultFriction, _simParams._defaultRestitution); } // ==== Define walker torso. float walkerX = 0f; float walkerY = 1.30f;// + ((float)_rng.NextDouble() * 0.1f); BodyDef torsoBodyDef = new BodyDef(); torsoBodyDef.Position.Set(walkerX, walkerY); torsoBodyDef.IsBullet = true; // Create walker torso. _torsoBody = _world.CreateBody(torsoBodyDef); PolygonDef torsoShapeDef = new PolygonDef(); torsoShapeDef.SetAsBox(0.10f, 0.30f); torsoShapeDef.Friction = _simParams._defaultFriction; torsoShapeDef.Restitution = 0f; torsoShapeDef.Density = 2f; torsoShapeDef.Filter.CategoryBits = 0x2; _torsoBody.CreateShape(torsoShapeDef); _torsoBody.SetMassFromShapes(); // ===== Create legs. // Leg joint definition. RevoluteJointDef jointDef = new RevoluteJointDef(); jointDef.CollideConnected = false; jointDef.EnableMotor = true; jointDef.MaxMotorTorque = 0f; // Other re-usable stuff . const float legRadius = 0.05f; // Half the thickness of the leg Vec2 upperLegPosBase = new Vec2(walkerX, walkerY - 0.25f); Vec2 lowerLegPosBase = new Vec2(walkerX, walkerY - 0.75f); // ===== Create left leg. // Upper leg. Body upperLeftLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to torso (hip joint) jointDef.Initialize(_torsoBody, upperLeftLegBody, upperLegPosBase); _leftHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef); // Lower leg. _leftLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to upper leg (knee joint) jointDef.Initialize(upperLeftLegBody, _leftLowerLegBody, lowerLegPosBase); _leftKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef); // ===== Create right leg. // Upper leg. Body upperRightLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to torso (hip joint) jointDef.Initialize(_torsoBody, upperRightLegBody, upperLegPosBase); _rightHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef); // Lower leg. _rightLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to upper leg (knee joint) jointDef.Initialize(upperRightLegBody, _rightLowerLegBody, lowerLegPosBase); _rightKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef); }