// // Roll a random number between (and including) 0 and randMax, and tell us if that number falls // between the lower and upper bounds or not. (attention: boundaries are included!) // // Zero = No. // One = Yes. // Negative return values are errors and generally mean that arguments make no sense // // This is useful for pathing decisions: Say that at point P in a script a choice has to be made // between continuing with actions A, B, and C. The decision is made based on a percentage: // A = 10% chance, B = 50% chance, C = 40% chance. This function was written to support the code // that would make this decision. // // Note: Mathematically speaking this approach has flaws. A better method would roll the number // just once, and then apply the boundary constraints to it repeatedly to make the decision. // This needs further work (tm). // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // example usage: // y_rand_in_sliding_window(1, 10, 20); // // Returns 1 if the random number rolled is 4, and 0 if the random number was 11. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int y_rand_in_sliding_window(int lowerbound, int upperbound, int randMax) { int roll; if( (0>lowerbound) || (lowerbound>upperbound) || (upperbound > randMax) || (randMax <= 0)) { lr_error_message("y_rand_in_sliding_window called with nonsensical arguments: ( 0 <= %d < %d <= %d ) == FALSE", lowerbound, upperbound, randMax); return -1; } roll = y_rand_between(0, randMax); if( (roll >= lowerbound) && (roll <= upperbound) ) { return 1; } return 0; }
/*! This function uses a given set of characters to create words, separated by spaces. The words are minimal \e minWordLength characters long, and maximum \e minWordLength characters. The total length of the line is minimal \e minimumLength and maimum \e maximumLength long. \b Example: \code // Generates a string of minimal 3 and max 20 characters, // with words of minimal 1 and maximal 3 charactes. // Chooses only characters a, c, d or d. y_random_string_buffer_core("uitvoer", 3,20, 1, 3, "abcd"); // Generates some sort of mock morse-code of exactly 30 characters. // with words of minimal 1 and maximal 3 charactes. // Chooses only characters a, c, d or d. y_random_string_buffer_core("uitvoer", 3,20, 1, 3, "abcd"); // could result in "ccc db dac c" \endcode @param[out] parameter Name of the LR-parameter in which the result is stored @param[in] minimumLength Minumum length of the string @param[in] maximumLength Maximum length of the string @param[in] minWordLength Minimum length of the words within the string @param[in] maxWordLength Minimum length of the words within the string @param[in] characterSet The string is build from this string of characters \return void \author Floris Kraak / Raymond de Jongh \sa y_random_number_buffer \sa y_random_string_buffer_curses \sa y_random_string_buffer \sa y_random_string_buffer_hex */ void y_random_string_buffer_core(const char *parameter, int minimumLength, int maximumLength, int minWordLength, int maxWordLength, char *characterSet) { char *buffer; int charSetSize; // length of the characterSet int length = 0; int max = -1; char randomNumber; int lettersInWord; charSetSize=strlen(characterSet); //lr_message("minimumLength %d -- maximumLength %d -- minWordLength %d -- maxWordLength %d", // minimumLength, maximumLength, minWordLength, maxWordLength); // error checks - lots of code that saves us headaches later if( minimumLength < 0 ) { lr_error_message( "minimumLength less than 0 (%d)", minimumLength ); } else if( maximumLength < 1 ) { lr_error_message( "maximumLength less than 1 (%d)", maximumLength ); } else if( maximumLength > (1024 * 1024) ) { lr_error_message( "maximumLength too big (%d)", maximumLength ); } else if( maximumLength < minimumLength ) { lr_error_message( "minimumLength (%d) bigger than maximumLength (%d)", minimumLength, maximumLength ); } else if(maximumLength > minimumLength) { // Not an error max = y_rand_between(minimumLength, maximumLength); lr_log_message("Max: %d", max); } else if(maximumLength == minimumLength) { // Not an error either max = maximumLength; } else { lr_error_message("This can never happen. If we reach this point it's a bug."); } // if we got an error if( max < 0 ) { lr_set_transaction_status(LR_FAIL); lr_exit(LR_EXIT_ITERATION_AND_CONTINUE, LR_FAIL); } // get memory for the buffer buffer = (char *)y_mem_alloc( max +1 ); // note: if this fails y_mem_alloc() aborts the script, so no error handling needed. while( length < max ) { // lr_message("Length: %d max: %d", length, max); // lettersInWord = ((y_rand() % 8) + 2); if( maxWordLength == 0 ) { lettersInWord = maximumLength; } else { lettersInWord = y_rand_between(minWordLength, maxWordLength); if( lettersInWord < 0 ) { lr_error_message( "y_rand_between() returned an errorcode (%d)", lettersInWord ); lr_set_transaction_status(LR_FAIL); lr_exit(LR_EXIT_ITERATION_AND_CONTINUE, LR_FAIL); } } while( lettersInWord-- && (length < (max)) ) { randomNumber = (char) (y_rand() % charSetSize); buffer[length++] = characterSet[randomNumber]; } if( maxWordLength != 0 ) { if( length < max -1 ) { buffer[length++] = ' '; } } } buffer[max] = '\0'; lr_save_string(buffer, parameter); free(buffer); }