Beispiel #1
0
    public List <ScatterPhoton> Emit(float duration)
    {
        List <ScatterPhoton> ret = new List <ScatterPhoton>();

        // Debug.LogFormat("Emitting energy {0}",energy);
        for (int i = 0; i < energy; i++)
        {
            Vector3 emitPos = location + radius * TrailmeshMaker.onUnitSphere();
            Vector3 perp    = new Vector3(-directionPreference.y, directionPreference.x, 0);
            if (directionPreference.z > 0.9)
            {
                perp = new Vector3(0, directionPreference.z, -directionPreference.y);
            }
            Quaternion rt = Quaternion.AngleAxis((float)RandomGen3.NextDouble() * 360, directionPreference);
            perp = (rt * perp).normalized;
            float         angl = Mathf.PI * (1 - directionBias) * (float)RandomGen3.NextDouble();
            Vector3       dir  = (directionPreference * Mathf.Cos(angl) + perp * Mathf.Sin(angl)).normalized;
            ScatterPhoton pht  = new ScatterPhoton();
            // Debug.LogFormat("Derection:{0} {1}", dir, angl);
            pht.direction  = dir;
            pht.position   = emitPos;
            pht.timeOffset = timeOffset + duration * (float)RandomGen3.NextDouble();
            ret.Add(pht);
        }
        //  Debug.LogFormat("Emitted energy {0}", energy);
        return(ret);
    }
Beispiel #2
0
    virtual public MultimeshObject Simulate()
    {
        int                  emittedPhotons = Mathf.RoundToInt(emissionRate * duration);
        MultimeshObject      ret            = new MultimeshObject();
        List <ScatterPhoton> emitted        = new List <ScatterPhoton>();

        Debug.LogFormat("Emitted track photons: {0} ", emittedPhotons);
        for (int i = 0; i < emittedPhotons; i++)
        {
            float   emitTime = (float)RandomGen3.NextDouble() * duration;
            Vector3 ppos     = emitTime * speed * direction + initialPos;
            emitted.Add(EmitCherenkovPhoton(ppos, emitTime));
            if (emitted.Count >= 16384)
            {
                ret.meshes.AddRange(PhotonsToMesh(emitted).meshes);
                emitted.Clear();
            }
        }
        ret.meshes.AddRange(PhotonsToMesh(emitted).meshes);
        if (energyLosses != null)
        {
            foreach (CascadeData dat in energyLosses)
            {
                emitted = dat.Emit(0.02f);
                ret.meshes.AddRange(PhotonsToMesh(emitted).meshes);
            }
        }
        Debug.LogFormat("Total meshes: {0} ", ret.meshes.Count);
        return(ret);
    }
Beispiel #3
0
        /// <summary>
        /// 获取随机的姓名
        /// </summary>
        public static string NextSurName()
        {
            string xing   = Xings[Random(0, Xings.Length)];
            string name   = string.Empty;
            int    length = RandomGen3.NextDouble() > 0.3 ? 2 : 1;

            for (int i = 0; i < length; i++)
            {
                name += Mings[Random(0, Mings.Length)];
            }
            return(xing + name);
        }
        public override void managedAccounts(string accountsList)
        {
            var splittedAccounts = accountsList.Split(',').Where(e => e.Length > 0).ToList();

            splittedAccounts.ForEach(account =>
            {
                var positionId  = RandomGen3.Next();
                var portfolioId = RandomGen3.Next();
                var pnlId       = RandomGen3.Next();

                links.TryAdd(positionId, account);
                links.TryAdd(portfolioId, account);
                links.TryAdd(pnlId, account);

                var newAccount = new Account()
                {
                    AccountName  = account,
                    PnLContainer = new PnLContainerEx()
                    {
                        Id = pnlId
                    },
                    PortfolioContainer = new PortfolioContainerEx()
                    {
                        Id = portfolioId
                    },
                    PositionContainer = new PositionContainerEx()
                    {
                        Id = positionId
                    },
                    OrderContainer = new OrderContainerEx()
                };

                accountList.TryAdd(account, newAccount);

                clientSocket.reqAccountUpdatesMulti(portfolioId, account, "", true);
                clientSocket.reqPnL(pnlId, account, "");
                clientSocket.reqPositionsMulti(positionId, account, "");
            });

            foreach (var elem in assetLoader.Elements)
            {
                GetContract(elem.Symbol, elem.SecType, elem.Exchange, elem.Currency);
            }

            clientSocket.reqMarketDataType(1);
            clientSocket.reqAllOpenOrders();
            clientSocket.reqIds(-1);
        }
Beispiel #5
0
    public static float DrawExp(float ilam)
    {
        if (ilam <= 0)
        {
            ilam = 0.01f;
        }
        float ran = (float)RandomGen3.NextDouble();

        float ret = -Mathf.Log(1.0f - ran) * ilam;

        if (ret > ilam * 10000)
        {
            return(ilam * 10000);
        }
        return(ret);
    }
Beispiel #6
0
 static public Vector3 onUnitSphere()
 {
     while (true)
     {
         double  theta = 2 * Math.PI * RandomGen3.NextDouble();
         double  phi   = Math.Acos(1 - 2 * RandomGen3.NextDouble());
         double  x     = Math.Sin(phi) * Math.Cos(theta);
         double  y     = Math.Sin(phi) * Math.Sin(theta);
         double  z     = Math.Cos(phi);
         Vector3 vct   = new Vector3((float)x, (float)y, (float)z);// (float)(rndgen.NextDouble() * 2 - 1), (float)(rndgen.NextDouble() * 2 - 1), (float)(rndgen.NextDouble() * 2 - 1));
         var     mag   = vct.magnitude;
         if (mag <= 1.000001 && mag > 0.0001f)
         {
             return(vct / mag);
         }
         // Debug.LogFormat("Rejected {0}", vct);
     }
 }
Beispiel #7
0
    public void Propagate(float absorbtion, float scatter)// scatter length
    {
        float distance = TrailmeshMaker.DrawExp(scatter);
        //Debug.LogFormat("Ran: {0} {1} {2}", distance, absorbtion, scatter);
        float dur       = distance / speed;
        float decayprob = 1.0f - Mathf.Exp(-distance / absorbtion);

        prevPosition = position;
        prevTime     = timeOffset;
        position     = position + direction * distance;
        timeOffset  += dur;
        if (RandomGen3.NextDouble() <= decayprob)
        {
            decayed = true;
            return;
        }
        direction = TrailmeshMaker.Scatter(direction);
    }
        public void GetContract(string code, string type, string exchange, string currency)
        {
            var contract = new Contract()
            {
                Symbol   = code,
                SecType  = type,
                Exchange = exchange,
                Currency = currency
            };

            int id = RandomGen3.Next();

            symbolPermission.TryAdd(code, true);


            /*****
            *  clientSocket.reqContractDetails(id, contract);
            *  clientSocket.reqMktData(id, contract, string.Empty, false, false, null);
            *****/
        }
Beispiel #9
0
    // Start is called before the first frame update

    public static Vector3 Scatter(Vector3 inc)
    {
        int cnt = 0;

        while (true)
        {
            Vector3 rd   = onUnitSphere();
            float   prob = scatterdirprob(inc, rd);

            if (prob >= RandomGen3.NextDouble() * 40)
            {
                //Debug.LogFormat("Prob: {0}",prob);
                return(rd);
            }
            if (cnt > 100)
            {
                return(inc);
            }
            cnt += 1;
        }
    }
Beispiel #10
0
    ScatterPhoton EmitCherenkovPhoton(Vector3 pos, float timeOffset)
    {
        float   shift = (float)RandomGen3.NextDouble();
        Vector3 perp  = new Vector3(-direction.y, direction.x, 0);

        if (Mathf.Abs(direction.z) > 0.9)
        {
            perp = new Vector3(0, direction.z, -direction.y);
        }
        Quaternion rt = Quaternion.AngleAxis(shift * 360, direction);

        perp  = rt * perp;
        perp /= perp.magnitude;
        Vector3       photondir = direction * cos41 + perp * sin41;
        ScatterPhoton ret       = new ScatterPhoton();

        ret.direction  = photondir;
        ret.position   = pos;
        ret.timeOffset = timeOffset;
        //ret.Propagate(IcecubeDust.getAbsorption(pos), IcecubeDust.getScatter(pos));
        return(ret);
    }
Beispiel #11
0
 /// <summary>
 /// 返回介于min和max之间的一个随机数。包括min,不包括max
 /// </summary>
 /// <param name="min"></param>
 /// <param name="max"></param>
 /// <returns></returns>
 public static int Random(int min, int max)
 {
     return(RandomGen3.Next(min, max));
 }
Beispiel #12
0
 /// <summary>
 /// 获取随机的民族
 /// </summary>
 public static string NextNationName()
 {
     return(RandomGen3.NextDouble() > 0.5 ? "汉族" : NationNames[Random(0, NationNames.Length)]);
 }
Beispiel #13
0
 /// <summary>
 /// 返回数组中的随机元素
 /// </summary>
 /// <typeparam name="T">元素类型</typeparam>
 /// <param name="random"></param>
 /// <param name="items">元素数组</param>
 /// <returns>元素数组中的某个随机项</returns>
 public static T NextItem <T>(T[] items)
 {
     return(items[RandomGen3.Next(0, items.Length)]);
 }
Beispiel #14
0
 /// <summary>
 /// 返回非负随机数(多线程下也严格随机)
 /// </summary>
 /// <returns></returns>
 public static int Random()
 {
     return(RandomGen3.Next());
 }
Beispiel #15
0
 /// <summary>
 /// 返回指定时间段内的随机时间值
 /// </summary>
 /// <param name="minValue">时间范围的最小值</param>
 /// <param name="maxValue">时间范围的最大值</param>
 /// <returns>指定时间段内的随机时间值</returns>
 public static DateTime Random(DateTime minValue, DateTime maxValue)
 {
     return(RandomGen3.NextDateTime(minValue, maxValue));
 }
        static void Main(string[] args)
        {
            Debug.WriteLine("main thread id " + Thread.CurrentThread.ManagedThreadId);

            List <int> lstNumbersToIterate = new List <int>();

            for (int i = 0; i < 1000000; i++)
            {
                lstNumbersToIterate.Add(i);
            }

            ParallelOptions paralelOp = new ParallelOptions();

            paralelOp.MaxDegreeOfParallelism = 10;
            //since we have 1m items and maximum 8 threads, the .net has split our list into 8 parts starting from 0, 125k, 250k, 375k, 500k, ..
            //Parallel.ForEach(lstNumbersToIterate, paralelOp, number => {

            //    //Debug.WriteLine("each iteration inside parallel foreach scope thread id " + Thread.CurrentThread.ManagedThreadId); each one runs in a task not in the main thread

            //    Console.WriteLine(number);

            //});


            Random rand = new Random();

            Parallel.For(0, 1000000, (i, loop) =>
            {
                if (rand.Next() == 0)
                {
                    loop.Stop();
                }
            });

            while (true)
            {
                Console.WriteLine(rand.Next());
                Console.ReadLine();
            }

            //int lastNumber = 1, count = 0;
            //Parallel.For(0, 1000000, (i, loop) =>
            //{
            //    // Get the next number
            //    int curNumber = new Random().Next();

            //    // If it’s the same as the previous number,
            //    // note the increased count
            //    if (curNumber == lastNumber) count++;

            //    // Otherwise, we have a different number than
            //    // the previous. Write out the previous number
            //    // and its count, then start
            //    // over with the new number.
            //    else
            //    {
            //        if (count > 0)
            //            Console.WriteLine(
            //                count + ": " + lastNumber);
            //        lastNumber = curNumber;

            //    }
            //});

            Stopwatch sw;
            const int NUM = 100000;

            StreamWriter swWrite = new StreamWriter("classic non locking random generator.txt");

            swWrite.AutoFlush = true;

            paralelOp.MaxDegreeOfParallelism = 100;

            List <int> lstGeneratedNumbers = new List <int>();

            while (true)
            {
                sw = Stopwatch.StartNew();
                Parallel.For(0, NUM, paralelOp, i =>
                {
                    lstGeneratedNumbers.Add(RandomGen0.Next());
                });
                Console.WriteLine(sw.Elapsed);


                File.WriteAllLines("0 static non locking random generator.txt", lstGeneratedNumbers.Select(p => p.ToString()));
                lstGeneratedNumbers = new List <int>();

                sw = Stopwatch.StartNew();
                var vrResult = Parallel.For(0, NUM, paralelOp, i =>
                {
                    lstGeneratedNumbers.Add(RandomGen1.Next());
                });
                Console.WriteLine(sw.Elapsed);



                File.WriteAllLines("1 thread static random generator.txt", lstGeneratedNumbers.Select(p => p.ToString()));
                lstGeneratedNumbers = new List <int>();

                sw = Stopwatch.StartNew();
                Parallel.For(0, NUM, paralelOp, i =>
                {
                    lstGeneratedNumbers.Add(RandomGen2.Next());
                });
                Console.WriteLine(sw.Elapsed);


                File.WriteAllLines("2 thread static and RNGCryptoServiceProvider random generator.txt", lstGeneratedNumbers.Select(p => p.ToString()));
                lstGeneratedNumbers = new List <int>();

                sw = Stopwatch.StartNew();
                Parallel.For(0, NUM, paralelOp, i =>
                {
                    lstGeneratedNumbers.Add(RandomGen3.Next());
                });
                Console.WriteLine(sw.Elapsed);


                File.WriteAllLines("3 thread local and RNGCryptoServiceProvider random generator.txt", lstGeneratedNumbers.Select(p => p.ToString()));
                lstGeneratedNumbers = new List <int>();

                sw = Stopwatch.StartNew();
                Parallel.For(0, NUM, paralelOp, i =>
                {
                    lstGeneratedNumbers.Add(RandomGen4.Next());
                });
                Console.WriteLine(sw.Elapsed);


                lstGeneratedNumbers = new List <int>();

                sw = Stopwatch.StartNew();
                Parallel.For(0, NUM, paralelOp, i =>
                {
                    lstGeneratedNumbers.Add(threadSafeRandomWithCrypto(0, Int32.MaxValue));
                });
                Console.WriteLine(sw.Elapsed);

                File.WriteAllLines("5 thread safe random generator.txt", lstGeneratedNumbers.Select(p => p.ToString()));

                swWrite.Flush();

                Console.ReadLine();
            }
        }