Random with a certain amount of a certain value

my problem is this: I need to create some random values ​​for planning. For example, process time is given. Assume that job j on a machine gets a random value between (1.99). this is the time required to operate this machine.

Now I need to manipulate these random values. I like to talk about all the random times of the process, 20% of which are zero processes. So does anyone know how to give an array with integers of a certain amount with a specific time?

here's the normal random:

p_machine_job_completionTime = new int[Constants.numberMachines][];
        for (i = 0; i < Constants.numberMachines; i++)
        {
            p_machine_job_completionTime[i] = new int[Constants.numberJobs];
            for (j = 0; j < Constants.numberJobs; j++)
            {
                p_machine_job_completionTime[i][j] = random.Next(1, 99);
            }
        }

Now the jobs can skip the machine and therefore have a processing time of 0. Is it possible to limit random values, ensuring that x% of all my random values ​​have a value of 0 ?? eg:

20% of p_machine_job_completionTime[i][j] = 0
80% of p_machine_job_completionTIme[i][j] = random (1,99)

.

+4
4

, - :

" , , x% 0"

, x% 0, , @Douglas. @Douglas, " 0 20%". , , 20% , 20%, 80% . , , . numberMachines numberJobs, .

int numberMachines = 5;
int numberJobs = 20;
Random random = new Random();
var p_machine_job_completionTime = new int[numberMachines][];
var theTwentyPercent = new HashSet<int>(Enumerable.Range(0,(numberJobs * numberMachines) -1 ).OrderBy(x => Guid.NewGuid()).Take(Convert.ToInt32((numberMachines * numberJobs) * 0.2)));

for (int i = 0; i < numberMachines; i++) {
    p_machine_job_completionTime[i] = new int[numberJobs];
    for (int j = 0; j < numberJobs; j++) {
        int index = (i * numberJobs) + j;
        if (theTwentyPercent.Contains(index)) {
            p_machine_job_completionTime[i][j] = 0;
        }
        else {
            p_machine_job_completionTime[i][j] = random.Next(1, 99);
        }
    }
}

Debug.Assert( p_machine_job_completionTime.SelectMany(x => x).Count(val => val==0) == (numberMachines * numberJobs) * 0.2 );
0

- : 0 () . , randoms , 0:

int val = random.Next(1, 123);
if (val >= 99)
    val = 0;

98 ( 1 98, ). 0 20%, 1/(1 - 20%) 125% , 123.

+1

: 20% 0 80%, 1..99

Random random;

...

int value = random.Next(5) == 0 ? 0 : random.Next(99) + 1;
+1

, Random . modulo for:

    p_machine_job_completionTime = new int[Constants.numberMachines][];
    int counter = 0;  
    for (i = 0; i < Constants.numberMachines; i++)
    {
        p_machine_job_completionTime[i] = new int[Constants.numberJobs];
        for (j = 0; j < Constants.numberJobs; j++)
        {
              if( counter%5 == 0)
              {
                 p_machine_job_completionTime[i][j] = 0;
              }
              else
              {
                 p_machine_job_completionTIme[i][j] = random (1,99);                         
              }
              counter++;
        }
    }
0
source

Source: https://habr.com/ru/post/1524129/


All Articles