Forum Moderators: coopster

Message Too Old, No Replies

Number of repetitions ? thoughts please

Whats the effecient way to approach this?

         

Matthew1980

7:01 pm on Jun 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

function randomizer($recurr){
$setOne = range('A','Z');
$setTwo = range(0,9);

$UniqueKeyArray = array_merge($setOne,$setTwo);
$number = "#".$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)]."#";

return $number;
}
for($i=0; $i<100; $i++){
echo randomizer('2')."<br/>";
}



As it is, this works fine, but I have decided to make it more flexible, so adding the int parameter into the mix, this basically means number of repetitions until the next random string is generated. Ideally I would just like it to do a maximum of 10 repetitions before it regenerates a new code - but, I don't want this returned as an array, just a string.

Thanks for any thoughts,

Cheers,
MRb

Readie

7:05 pm on Jun 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for($i = 0; $i < 100; $i++) {
$code = ($i % 10)? $code : randomizer('2');
echo $code . '<br>';
}

Is what you want to do I think?

Matthew1980

7:34 pm on Jun 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie,

Cheers for the suggestion, but I have just found a fundamental flaw in the example that I posted, the value can't/shouldn't be passed into the function, on multiple calls it would not function as I intended it to. Into context, this is a private function within a class, so I need to set its repetitions within the class as a property, not outside as user defined, and as it's private, I would get an error anyway.

private function randomizer(){
$setOne = range('A','Z');
$setTwo = range(0,9);

$UniqueKeyArray = array_merge($setOne,$setTwo);
$number = "#".$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)].$UniqueKeyArray[rand(0,35)]."#";

return $this->number;
}

So the return value needs to be set to either 1,2,3 or 4 repetitions of the same code before generating a new unique number from within the function - I hope that now makes sense. ;)

I shall keep trying out idea's but I seem to have writers block tonight :(

Cheers,
MRb