Re: Random Number Excluding Specified Value in Array



AP wrote:
Hello
Any suggestions on the most efficient way to generate a random number
from a range that will not return values specified in an array?


Euhm, I am not really sure what you mean. Like this?

For integers:
function
rand_number_without_certain_values_for_some_reason($start,$end,$not){
$i = 0;
do{
$i++;
if($i > 5000) return false;
$int = mt_renc($start,$end);
} while (is_array($not) && in_array($int,$not));
return $int;
}

Had to build in safety (the $i) to avoid endless looping when using
something like (1,4,array(range(1,4)))

Grtz,
--
Rik Wasmus


.



Relevant Pages

  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Strategy or Iterator?
    ... It would be possible to write a class that returns the variations ... GNU General Public License for more details. ... protected CombinatoricOperator(Telements, int r) { ... An integer array backing up the original one to keep track of the ...
    (comp.lang.java.programmer)
  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Memory Allocation Problem, please help
    ... typedef struct word_tag{ ... array is not an array. ... static int total_word_count; ... static int word_index(const char *word); ...
    (comp.lang.c)