0

I'm trying to implement my Processing code so that the line of the code will NOT run 5% of the total number of the times that a line of the code is supposed to run. But the crucial additional point is that the instance that the line would not be running should be unpredictable (random).

For instance, let's say that the code is to run 20 times. I want the code to not run in one of the 20 instances. This one instance would be 5% of the total (20) [1/20=5%].

This has to be random so that for example, in the first iteration of 20, the code does not run in the 3rd trial, and in the second iteration, the line does not run in the 10th trial.

This is the illustration:

First iteration: [1, 2, 3 (Don't run), 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Second iteration: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (Don't run), 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Third iteration:.

I wrote the pseudocode below but wonder whether there is a better way of doing this.

create an array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] 

take a number out of a set randomly and if the number is NOT 1:
    println("run")    
    get rid of the selected number within an array
if the set is empty, get a new set
Peter O.
  • 32,158
  • 14
  • 82
  • 96
John
  • 1
  • 1
    To be clear, the number of times the code is run (in this case 20) is known beforehand and not random? – Qwertie Jan 16 '20 at 02:25
  • @Qwertie The number of the total times that it will run is undetermined but we'll divide the total number by 20. So if the code will run in blocks of 20 and determine a target number (when the code will run) whenever it has to go through a new 20 block. – John Jan 17 '20 at 03:51

1 Answers1

0

The critical question you need to ask is whether the code is allowed to fail to run more than once. You have a 5% change of landing on 1 the first iteration, but you also have a 5% chance of landing on 1 in the subsequent iterations if the number is not removed. Is this acceptable? If not, simply remove the 'catch' value after it is landed upon.

There is also a slightly shorter way of illustrating such a selection, and that would be through modulo:

generate a random number:
    divide the number by 20
    check that the modulo is equal to 0

Which would look something like:

rand() % 20 == 0

With this approach, it would be more efficient to set a flag denoting that the failed run conditional had been met, and stop treating subsequent runs as failures, even if they land on the same modulo:

generate a random number
if not failed yet:
    if random number divided by 20 has a modulo of 0:
        denote a failure

Or as code:

if (not failed_run) {
    if (rand() % 20 == 0) {
        failed_run = true
    }
}

Also note that there is slight weighting distribution bias when checking a random number modulo. This will be more problematic the smaller the sample size is (20 in this case), so may be worth taking into consideration.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71