dimanche 18 octobre 2015

very strange random error c# [duplicate]

This question already has an answer here:

Morning guys, this explanation is gonna be quite long as it is really strange and i will have to explain the whole scenario to u guys.

I am writing a program that will run a simulation for a business for N number of days. the business receive 0 to 4 customers per day and each customer buys 1 to 4 tires when visiting so if there are 2 customers the minimum tires sold is 2 and the max is 8. both the tires sold and the customers visiting is generated by n random number.

i receive a random number out of a 100 then use probability to assign it to a value of 0 to 4 for customers and 1 to 4 for tires. for example i say the chance are 0 customers 10%, 1 - 30%, 2 - 30%, 3 - 20%, 4 - 10%. then i count it up and if its between 0 and 10 its 0 customers , between 10 and 40 its 2 customers etc.

i run this in a loop then use that customers and detirmine how many tires was sold in total that day. Now my error.

here is my method for doing that :

public int tires(int cutomers)
    {
        int randomtires = 0, total = 0;
        Random rnd1 = new Random();

        for (int i = 0; i < 4; i++)
        {

            randomtires = rnd1.Next(1, 101);

            if (randomtires <= 40 && randomtires > 0)
            {
                amounttires[i] = 1;
            }
            else if (randomtires <= 60 && randomtires > 40)
            {
                amounttires[i] = 2;
            }
            else if (randomtires <= 70 && randomtires > 60)
            {
                amounttires[i] = 3;
            }
            else
            {
                amounttires[i] = 4;
            }
        }

        if (cutomers == 0)
        {
            total = 0;
        }
        else if (cutomers == 1)
        {
            total = amounttires[0];
        }
        else if (cutomers == 2)
        {
            total = amounttires[0] + amounttires[1];
        }
        else if (cutomers == 3)
        {
            total = amounttires[0] + amounttires[1] + amounttires[2];
        }
        else
        {
            total = amounttires[0] + amounttires[1] + amounttires[2] + amounttires[3];
        }


        //MessageBox.Show("" + amounttires[0] + amounttires[1] + amounttires[2] + amounttires[3]);
        return total;



    }

According to me this should word as i run a for loop for the amount days and each loop i call this method which should mean if day 1 has 2 customers and day 2 has 2 customers the amount of tires should differ ( most likely) but its as if it retains its first set of data through all the days, lets say it runs 20 days, all the days with 2 customers sells the same amount of tires and the same with the other amount of customers it does not differ.

here is the strange part, if i display the amount of tires array ( which hold amount of tires for how many customers arrive that day) in a message box each loop then it works correct and randomizes the tires sold correctly ... this is really breaking my brain.

this is a long explanation please ask if something is unclear about my problem

regards MadJar




Aucun commentaire:

Enregistrer un commentaire