public override double GenerateValue() { var primer = Math.Sqrt(-(2 * Math.Log(UniformRandom.NextDouble(), Math.E))); var sen = Math.Sin(2 * Math.PI * UniformRandom.NextDouble()); return(Math.Sqrt(S2) * primer * sen + M); }
public static Vector3 CalculateShotingDir(int seed, float yaw, float pitch, float spreadX, float spreadY) { Quaternion q = Quaternion.Euler(pitch, yaw, 0); Vector3 forward = q.Forward(); Vector3 right = q.Right(); Vector3 up = q.Up(); float x; float y; //,z; x = (float)UniformRandom.RandomFloat(seed + 0, -0.5, 0.5) + (float)UniformRandom.RandomFloat(seed + 1, -0.5, 0.5); y = (float)UniformRandom.RandomFloat(seed + 2, -0.5, 0.5) + (float)UniformRandom.RandomFloat(seed + 3, -0.5, 0.5); //z = x * x + y * y; float res1 = spreadX * x; float res2 = spreadY * y; right = Vector3Ext.Scale(right, res1); up = Vector3Ext.Scale(up, res2); var newForward = Vector3Ext.Add(forward, right); newForward = Vector3Ext.Add(newForward, up); //_logger.InfoFormat("spreadX {0}, spreadY {1}, x {2} y {3}, right{4}, up{5}", spreadX, spreadY, x, y, right, up); return(newForward); }
private void BaseKickBack(PlayerWeaponController controller, int seed, WeaponConfigNs.Kickback kickbackParams, float hOffsetFactor, float vOffsetFactor) { var commonFireConfig = controller.HeldWeaponAgent.CommonFireCfg; var kickbackConfig = controller.HeldWeaponAgent.RifleKickbackLogicCfg; var weaponState = controller.HeldWeaponAgent.RunTimeComponent; float kickUp; float kickLateral; if (weaponState.ContinuesShootCount == 1) { kickUp = kickbackParams.UpBase; kickLateral = kickbackParams.LateralBase; } else { kickUp = kickbackParams.UpBase + weaponState.ContinuesShootCount * kickbackParams.UpModifier; kickLateral = kickbackParams.LateralBase + weaponState.ContinuesShootCount * kickbackParams.LateralModifier; } var punchYaw = controller.RelatedOrient.NegPunchYaw; var punchPitch = controller.RelatedOrient.NegPunchPitch; var isMaxUp = false; punchPitch += kickUp; if (punchPitch > kickbackParams.UpMax + commonFireConfig.AttackInterval * 0.01f) { punchPitch = kickbackParams.UpMax; isMaxUp = true; } if (weaponState.PunchYawLeftSide) { punchYaw += kickLateral; if (punchYaw > kickbackParams.LateralMax) { punchYaw = kickbackParams.LateralMax; } } else { punchYaw -= kickLateral; if (punchYaw < -1 * kickbackParams.LateralMax) { punchYaw = -1 * kickbackParams.LateralMax; } } if (UniformRandom.RandomInt(seed, 0, (int)kickbackParams.LateralTurnback) == 0) { weaponState.PunchYawLeftSide = !weaponState.PunchYawLeftSide; } //if (isMaxUp) weaponState.PunchDecayCdTime = GetDecayCdTime(controller); weaponState.PunchPitchSpeed = (punchPitch - controller.RelatedOrient.NegPunchPitch) / weaponState.PunchDecayCdTime; weaponState.PunchYawSpeed = (punchYaw - controller.RelatedOrient.NegPunchYaw) / weaponState.PunchDecayCdTime; }
public Memory(int blocksCount, double lowerBoundReProc, double upperBoundReProc) { Blocks = blocksCount; FreeBlocks = Blocks; MaxBusyBlocks = 0; Time = 0; MemQueue = new QueueEx(); _rand = new UniformRandom(lowerBoundReProc, upperBoundReProc); }
public RectZone(float x, float y, float width, float height, RandomBase randomX, RandomBase randomY) { if (randomX == null) { randomX = new UniformRandom(); } if (randomY == null) { randomY = new UniformRandom(); } _x = x; _y = y; RandomX = randomX; RandomY = randomY; Width = width; Height = height; }
/// <summary>コンストラクタ</summary> public Uniform(Tensor tensor, Random random, float min = 0f, float max = 1f) : base(tensor) { this.generator = new UniformRandom(tensor.Shape, random); if (min != 0f || max != 1f) { InputNode inputnode = tensor; VariableNode node = inputnode * (max - min) + min; node.Update(inputnode); this.flow = Flow.FromInputs(inputnode); } else { this.flow = null; } this.Min = min; this.Max = max; }
public void CheckOrderStatistics03() { double lower = -10; double upper = 0; UniformRandom rand = new UniformRandom(lower, upper, new Random()); double[] values = new double[maxNumbers]; for (int i = 0; i < maxNumbers; i++) { values[i] = rand.Next(); Assert.LessOrEqual(lower, values[i], "The value should be higher than lower"); Assert.GreaterOrEqual(upper, values[i], "The value should be lower than higher"); } double average = values.Average(); Assert.AreEqual((lower + upper) / 2, average, tolerance * 10); double sumOfSquaresOfDifferences = values.Select(val => (val - average) * (val - average)).Sum(); double variance = sumOfSquaresOfDifferences / values.Length; Assert.AreEqual((upper - lower) * (upper - lower) / 12, variance, tolerance); }
public void SpeedTest() { int length = 65535 * 16; Shape shape = Shape.Vector(length); OverflowCheckedTensor v1 = new OverflowCheckedTensor(shape); UniformRandom ope = new UniformRandom(shape, new Random(1234)); Stopwatch sw = new Stopwatch(); sw.Start(); ope.Execute(v1); ope.Execute(v1); ope.Execute(v1); ope.Execute(v1); sw.Stop(); Console.WriteLine($"{sw.ElapsedMilliseconds / 4} msec"); }
protected Vector3 CalculateShotingDir(int seed, float yaw, float pitch, float spreadX, float spreadY) { Quaternion q = Quaternion.Euler(pitch, yaw, 0); Vector3 forward = q.Forward(); Vector3 right = q.Right(); Vector3 up = q.Up(); float x; float y; x = (float)UniformRandom.RandomFloat(seed + 0, -0.5, 0.5) + (float)UniformRandom.RandomFloat(seed + 1, -0.5, 0.5); y = (float)UniformRandom.RandomFloat(seed + 2, -0.5, 0.5) + (float)UniformRandom.RandomFloat(seed + 3, -0.5, 0.5); float res1 = spreadX * x; float res2 = spreadY * y; right = Vector3Ext.Scale(right, res1); up = Vector3Ext.Scale(up, res2); var newForward = Vector3Ext.Add(forward, right); newForward = Vector3Ext.Add(newForward, up); return(newForward); }
public TransactGenerator(double lowerBound, double upperBound) { Time = 0; _rand = new UniformRandom(lowerBound, upperBound); }
/// <summary> /// last/NewAccPunchPitch->TargetPunchPitchDelta /// </summary> /// <param name="heldAgent"></param> /// <param name="seed"></param> /// <param name="shakeInfo"></param> private void CalcBaseShake(WeaponAttackProxy attackProxy, int seed, ShakeInfo shakeInfo) { var runTimeComponent = attackProxy.RuntimeComponent; var orient = attackProxy.Orientation; var commonFireConfig = attackProxy.WeaponConfigAssy.S_CommonFireCfg; ShakeInfoStruct dirShakeArgs = FireShakeProvider.GetFireUpDirShakeArgs(attackProxy, shakeInfo); /*计算水平,垂直震动增量*/ float upDirShakeDelta, lateralDirShakeDelta; upDirShakeDelta = FireShakeFormula.CalcFireDirShakeDelta(dirShakeArgs.UpBase, dirShakeArgs.UpModifier, runTimeComponent.ContinuesShootCount); lateralDirShakeDelta = FireShakeFormula.CalcFireDirShakeDelta(dirShakeArgs.LateralBase, dirShakeArgs.LateralModifier, runTimeComponent.ContinuesShootCount); /*应用水平,垂直震动增量*/ float punchYaw, punchPitch; punchYaw = orient.AccPunchYaw; punchPitch = orient.AccPunchPitch; //垂直震动增量应用于punchPitch punchPitch = FireShakeFormula.CalcPunchPitch(punchPitch, upDirShakeDelta, dirShakeArgs.UpMax, commonFireConfig.AttackInterval * 0.01f); runTimeComponent.TargetPunchPitchDelta = punchPitch - orient.AccPunchPitch; //水平震动增量应用于punchYaw punchYaw = FireShakeFormula.CaclPunchYaw(runTimeComponent.PunchYawLeftSide, punchYaw, lateralDirShakeDelta, dirShakeArgs.LateralMax); /*应用于WeaponRuntimeComponent*/ //apply PunchYawLeftSide if (UniformRandom.RandomInt(seed, 0, (int)dirShakeArgs.LateralTurnback) == 0) { runTimeComponent.PunchYawLeftSide = !runTimeComponent.PunchYawLeftSide; } //apply PunchDecayCdTime runTimeComponent.PunchDecayLeftInterval = (int)FireShakeProvider.GetDecayInterval(attackProxy.WeaponConfigAssy); //PunchYawSpeed runTimeComponent.PunchYawSpeed = FireShakeFormula.CalcPitchSpeed(punchYaw, orient.AccPunchYaw, runTimeComponent.PunchDecayLeftInterval); //PunchPitchSpeed(Not Speed) var fireRollCfg = attackProxy.WeaponConfigAssy.S_FireRollCfg; if (fireRollCfg != null) { var rotation = orient.FireRoll; var rotateYaw = dirShakeArgs.UpBase * fireRollCfg.FireRollFactor; runTimeComponent.CameraRotationInterval = fireRollCfg.FireRollTime; if (Random.Range(0, 2) == 0) { rotateYaw = -rotateYaw; } var maxFireRollAngle = fireRollCfg.MaxFireRollAngle; if (rotation + rotateYaw >= maxFireRollAngle) { runTimeComponent.CameraRotationSpeed = (maxFireRollAngle - rotation) / runTimeComponent.CameraRotationInterval; } else if (rotation + rotateYaw <= -maxFireRollAngle) { runTimeComponent.CameraRotationSpeed = (-maxFireRollAngle - rotation) / runTimeComponent.CameraRotationInterval; } else { runTimeComponent.CameraRotationSpeed = rotateYaw / runTimeComponent.CameraRotationInterval; } } }
public void ExecuteTest() { int length = 65535 * 15; Shape shape = Shape.Vector(length); OverflowCheckedTensor v1 = new OverflowCheckedTensor(shape); UniformRandom ope = new UniformRandom(shape, new Random(1234)); ope.Execute(v1); float[] y = v1.State; Assert.IsTrue(y.Min() >= 0); Assert.IsTrue(y.Max() < 1); { int[] cnt = new int[10]; for (int i = 0; i < length; i++) { cnt[(int)(y[i] * 10)]++; } for (int i = 0; i < 10; i++) { Assert.AreEqual(0.1, (double)cnt[i] / length, 2e-3, $"[ {i * 0.1f}, {(i + 1) * 0.1f} )"); } } { int[] cnt = new int[20]; for (int i = 1; i < length; i++) { float dy = y[i - 1] - y[i]; cnt[(int)Math.Floor(dy * 10 + 10)]++; } Assert.AreEqual(0.005, (double)cnt[0] / (length - 1), 5e-3); Assert.AreEqual(0.015, (double)cnt[1] / (length - 1), 5e-3); Assert.AreEqual(0.025, (double)cnt[2] / (length - 1), 5e-3); Assert.AreEqual(0.035, (double)cnt[3] / (length - 1), 5e-3); Assert.AreEqual(0.045, (double)cnt[4] / (length - 1), 5e-3); Assert.AreEqual(0.055, (double)cnt[5] / (length - 1), 5e-3); Assert.AreEqual(0.065, (double)cnt[6] / (length - 1), 5e-3); Assert.AreEqual(0.075, (double)cnt[7] / (length - 1), 5e-3); Assert.AreEqual(0.085, (double)cnt[8] / (length - 1), 5e-3); Assert.AreEqual(0.095, (double)cnt[9] / (length - 1), 5e-3); Assert.AreEqual(0.095, (double)cnt[10] / (length - 1), 5e-3); Assert.AreEqual(0.085, (double)cnt[11] / (length - 1), 5e-3); Assert.AreEqual(0.075, (double)cnt[12] / (length - 1), 5e-3); Assert.AreEqual(0.065, (double)cnt[13] / (length - 1), 5e-3); Assert.AreEqual(0.055, (double)cnt[14] / (length - 1), 5e-3); Assert.AreEqual(0.045, (double)cnt[15] / (length - 1), 5e-3); Assert.AreEqual(0.035, (double)cnt[16] / (length - 1), 5e-3); Assert.AreEqual(0.025, (double)cnt[17] / (length - 1), 5e-3); Assert.AreEqual(0.015, (double)cnt[18] / (length - 1), 5e-3); Assert.AreEqual(0.005, (double)cnt[19] / (length - 1), 5e-3); } { int[] cnt = new int[20]; int sft = 1024; for (int i = sft; i < length; i++) { float dy = y[i - sft] - y[i]; cnt[(int)Math.Floor(dy * 10 + 10)]++; } Assert.AreEqual(0.005, (double)cnt[0] / (length - sft), 5e-3); Assert.AreEqual(0.015, (double)cnt[1] / (length - sft), 5e-3); Assert.AreEqual(0.025, (double)cnt[2] / (length - sft), 5e-3); Assert.AreEqual(0.035, (double)cnt[3] / (length - sft), 5e-3); Assert.AreEqual(0.045, (double)cnt[4] / (length - sft), 5e-3); Assert.AreEqual(0.055, (double)cnt[5] / (length - sft), 5e-3); Assert.AreEqual(0.065, (double)cnt[6] / (length - sft), 5e-3); Assert.AreEqual(0.075, (double)cnt[7] / (length - sft), 5e-3); Assert.AreEqual(0.085, (double)cnt[8] / (length - sft), 5e-3); Assert.AreEqual(0.095, (double)cnt[9] / (length - sft), 5e-3); Assert.AreEqual(0.095, (double)cnt[10] / (length - sft), 5e-3); Assert.AreEqual(0.085, (double)cnt[11] / (length - sft), 5e-3); Assert.AreEqual(0.075, (double)cnt[12] / (length - sft), 5e-3); Assert.AreEqual(0.065, (double)cnt[13] / (length - sft), 5e-3); Assert.AreEqual(0.055, (double)cnt[14] / (length - sft), 5e-3); Assert.AreEqual(0.045, (double)cnt[15] / (length - sft), 5e-3); Assert.AreEqual(0.035, (double)cnt[16] / (length - sft), 5e-3); Assert.AreEqual(0.025, (double)cnt[17] / (length - sft), 5e-3); Assert.AreEqual(0.015, (double)cnt[18] / (length - sft), 5e-3); Assert.AreEqual(0.005, (double)cnt[19] / (length - sft), 5e-3); } }
public override double GenerateValue() { return(-(1 / L * Math.Log(UniformRandom.NextDouble(), Math.E))); }
private void BaseKickBack(IPlayerWeaponState playerWeapon, int seed, float upBase, float lateralBase, float upModifier, float lateralModifier, float upMax, float lateralMax, float directionChang, float hOffsetFactor, float vOffsetFactor) { float kickUp; float kickLateral; if (playerWeapon.ContinuesShootCount == 1) { kickUp = upBase; kickLateral = lateralBase; } else { kickUp = upBase + playerWeapon.ContinuesShootCount * upModifier; kickLateral = lateralBase + playerWeapon.ContinuesShootCount * lateralModifier; } var punchYaw = playerWeapon.NegPunchYaw; var punchPitch = playerWeapon.NegPunchPitch; var isMaxUp = false; punchPitch += kickUp; if (punchPitch > upMax + _common.AttackInterval * 0.01f) { punchPitch = upMax; isMaxUp = true; } if (playerWeapon.PunchYawLeftSide) { punchYaw += kickLateral; if (punchYaw > lateralMax) { punchYaw = lateralMax; } } else { punchYaw -= kickLateral; if (punchYaw < -1 * lateralMax) { punchYaw = -1 * lateralMax; } } if (UniformRandom.RandomInt(seed, 0, (int)directionChang) == 0) { playerWeapon.PunchYawLeftSide = !playerWeapon.PunchYawLeftSide; } //if (isMaxUp) playerWeapon.PunchDecayCdTime = (int)(_common.AttackInterval * _config.DecaytimeFactor); playerWeapon.NegPunchYaw = punchYaw; playerWeapon.NegPunchPitch = punchPitch; playerWeapon.WeaponPunchPitch = punchPitch * vOffsetFactor; playerWeapon.WeaponPunchYaw = punchYaw * hOffsetFactor; Logger.DebugFormat("yaw src {0} new {1} pithc src {2} new {3}", playerWeapon.NegPunchYaw, playerWeapon.WeaponPunchYaw, playerWeapon.NegPunchPitch, playerWeapon.WeaponPunchPitch); }
static void Main(string[] args) { Console.WriteLine("Exponential Random:\n"); Console.Write("Enter lambda: "); string lambdaLine = Console.ReadLine(); IRandom random = new ExponentialRandom(); if (double.TryParse(lambdaLine, out double lambda)) { random = new ExponentialRandom(lambda); } List <double> list = new List <double>(10000); for (int i = 0; i < 10000; i++) { list.Add(random.Next()); } Show(list); random.DistributionStatistics(); DistributionStatistics(list); Console.WriteLine(" chi-square: " + random.IndificateDistributionLaw(list).ToString("F3")); Console.WriteLine("\nUniform Random:\n"); random = new UniformRandom(); list = new List <double>(10000); for (int i = 0; i < 10000; i++) { list.Add(random.Next()); } Show(list); random.DistributionStatistics(); DistributionStatistics(list); Console.WriteLine(" chi-square: " + random.IndificateDistributionLaw(list).ToString("F3")); Console.WriteLine("\nGauss Random:\n"); Console.Write("Enter sigma: "); string sigmaLine = Console.ReadLine(); Console.Write("Enter alpha: "); string alphaLine = Console.ReadLine(); random = new GaussRandom(); if (double.TryParse(sigmaLine, out double sigma) && double.TryParse(alphaLine, out double alpha)) { random = new GaussRandom(sigma, alpha); } list = new List <double>(10000); for (int i = 0; i < 10000; i++) { list.Add(random.Next()); } Show(list); random.DistributionStatistics(); DistributionStatistics(list); Console.WriteLine(" chi-square: " + random.IndificateDistributionLaw(list).ToString("F3")); Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); }
public override double GenerateValue() { return(A + (B - A) * UniformRandom.NextDouble()); }
private void CalcBaseShake(WeaponBaseAgent heldAgent, int seed, ShakeInfo shakeInfo) { var weaponController = heldAgent.Owner.WeaponController(); var runTimeComponent = heldAgent.RunTimeComponent; var orient = weaponController.RelatedOrientation; heldAgent.SyncParts(); var commonFireConfig = heldAgent.CommonFireCfg; ShakeInfoStruct dirShakeArgs = FireShakeProvider.GetFireUpDirShakeArgs(heldAgent, shakeInfo); /*计算水平,垂直震动增量*/ float upDirShakeDelta, lateralDirShakeDelta; upDirShakeDelta = FireShakeFormula.CalcFireDirShakeDelta(dirShakeArgs.UpBase, dirShakeArgs.UpModifier, runTimeComponent.ContinuesShootCount); lateralDirShakeDelta = FireShakeFormula.CalcFireDirShakeDelta(dirShakeArgs.LateralBase, dirShakeArgs.LateralModifier, runTimeComponent.ContinuesShootCount); /*应用水平,垂直震动增量*/ float punchYaw, punchPitch; punchYaw = orient.AccPunchYaw; punchPitch = orient.AccPunchPitch; //垂直震动增量应用于punchPitch punchPitch = FireShakeFormula.CalcPunchPitch(punchPitch, upDirShakeDelta, dirShakeArgs.UpMax, commonFireConfig.AttackInterval * 0.01f); runTimeComponent.TargetPunchPitchDelta = punchPitch - orient.AccPunchPitch; //水平震动增量应用于punchYaw punchYaw = FireShakeFormula.CaclPunchYaw(runTimeComponent.PunchYawLeftSide, punchYaw, lateralDirShakeDelta, dirShakeArgs.LateralMax); /*应用于WeaponRuntimeComponent*/ //apply PunchYawLeftSide if (UniformRandom.RandomInt(seed, 0, (int)dirShakeArgs.LateralTurnback) == 0) { runTimeComponent.PunchYawLeftSide = !runTimeComponent.PunchYawLeftSide; } //apply PunchDecayCdTime runTimeComponent.PunchDecayLeftInterval = (int)FireShakeProvider.GetDecayInterval(heldAgent); //PunchYawSpeed runTimeComponent.PunchYawSpeed = FireShakeFormula.CalcPitchSpeed(punchYaw, orient.AccPunchYaw, runTimeComponent.PunchDecayLeftInterval); //PunchPitchSpeed(Not Speed) var rotation = orient.FireRoll; var rotateYaw = CalculateRotationDegree(heldAgent, dirShakeArgs); if (UnityEngine.Random.Range(0, 2) == 0) { rotateYaw = -rotateYaw; } if (rotation + rotateYaw >= 3) { runTimeComponent.CameraRotationSpeed = (3 - rotation) / runTimeComponent.PunchDecayLeftInterval; } else if (rotation + rotateYaw <= -3) { runTimeComponent.CameraRotationSpeed = (-3 - rotation) / runTimeComponent.PunchDecayLeftInterval; } else { runTimeComponent.CameraRotationSpeed = rotateYaw / runTimeComponent.PunchDecayLeftInterval; } }