This function reads in the Hutchinson operator and corresponding probabilities and returns a randomly selected transform This works by choosing a random number and then iterating through all probabilities until it finds an appropriate bin
| 30 | // This works by choosing a random number and then iterating through all |
| 31 | // probabilities until it finds an appropriate bin |
| 32 | struct matrix select_array(struct matrix *hutchinson_op, double *probabilities, |
| 33 | size_t num_op) |
| 34 | { |
| 35 | // random number to be binned |
| 36 | double rnd = (double)rand() / RAND_MAX; |
| 37 | |
| 38 | // This checks to see if a random number is in a bin, if not, that |
| 39 | // probability is subtracted from the random number and we check the next |
| 40 | // bin in the list |
| 41 | for (size_t i = 0; i < num_op; ++i) { |
| 42 | if (rnd < probabilities[i]) { |
| 43 | return hutchinson_op[i]; |
| 44 | } |
| 45 | rnd -= probabilities[i]; |
| 46 | } |
| 47 | return hutchinson_op[0]; |
| 48 | } |
| 49 | |
| 50 | // This is a general function to simulate a chaos game |
| 51 | // - output_points: pointer to an initialized output array |