jeudi 20 décembre 2018

Generatic same random [duplicate]

I need my threads to generate different randoms every time, but some are not that random, as they're the same as previous ones. Already saw some answers about this but I'm not familiar with c# and the way i have the code is not fully compatible with it. Please note that I'm starting c# and even tho it could be simple, to adapt, i just don't know how.

Thank you for your time!

I've tried to use

    static int seed = Environment.TickCount;
    static readonly ThreadLocal<Random> random =
    new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));

But without success... Don't really know where i should use it. This is my code for now.

    for (var i = 0; i <= loopTo; i++)
        {
            var trd = new Thread(() =>
            {
                while (go != 0)
                {
                    try
                    {
                        int count = 0;
                        string c1 = "";
                        string pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
                        Random cc = new Random();
                        int strpos = 0;
                        while (count <= 3)
                        {
                            strpos = cc.Next(0, pool.Length);

                            c1 = c1 + pool[strpos];
                            count = count + 1;
                        }

                        // SOME MORE CODE
                    }
                    catch (WebException ex)
                    {

                    }
                }
            });
            trd.IsBackground = true;
            threads.Enqueue(trd);
            trd.Start();
        }




Aucun commentaire:

Enregistrer un commentaire