dimanche 31 mai 2020

C# problem with Random number in several lists [duplicate]

I was "fighting" with Random generator through some time in multi thread app on forms and I realize that Random isn't "thread safe".

Either way I need to use it in my app while I create an object of "Seller". Each Seller got his own stock with the same phones, but different price and amount.

Firstly I was trying to generate random amounts in stock when I started job of thread, but it returns me the same values in all stocks(I use Json to store data).

Then I decided to parse json data in Seller constructor and generate Random stock while creating an instance of this object. When I decided to create for example 5 objects, only in 1st stock values was different - in the rest 4 was the same.

Is any chance to solve it?

This is my Seller class:

 class Seller
    {
        private int ID;

        public List<Phone> Stock;
        private Phone phone;
        public int SellerCounter = 0;

        public Seller(int ID)
        {
            this.ID = ID;
            this.Stock = new List<Phone>();
            LoadJson();
            randomStock(this.Stock);
            string phonesJson = JsonConvert.SerializeObject(this.Stock);
            createStockFile(phonesJson);
            this.SellerCounter++;
        }

        public int getID()
        {
            return this.ID;
        }

        public void setID(int ID)
        {
            this.ID = ID;
        }

        public void LoadJson()
        {
            using (StreamReader r = new StreamReader("telefony.json"))
            {
                string json = r.ReadToEnd();
                this.Stock = JsonConvert.DeserializeObject<List<Phone>>(json);
            }
        }

        public void createStockFile(string phonesJson)
        {
            string fileName = "sellerStock" + this.getID()+".json";

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            File.WriteAllText(fileName, phonesJson);

        }

        public void randomStock(List<Phone> Stock)
        {

            double price = 0;

            double percent = 0;

            int amount = 0;

            Random random = new Random();

            foreach (Phone phone in Stock)
            {
                amount = random.Next(0, 15);

                price = phone.Get_OriginalPrice();

                percent = price * 0.1;

               if (random.Next(0, 2) == 0)
               {
                    price -= percent*0.1;
                    phone.Set_OriginalPrice(price);
               }
                else
                {
                    price += percent*0.1;
                    phone.Set_OriginalPrice(price);
                }
                phone.Set_SoldAmount(amount);
            }
        }

        public void setStock(List<Phone> phones)
        {
            foreach (Phone phone in phones)
            {
                this.Stock.Add(phone);
            }
        }

I know that I shouldn't do that in a constructor, but I need to find a solution...




Aucun commentaire:

Enregistrer un commentaire