This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi I have something like below where each time one random value gets pulled in for use.
What I want is to make sure no two same random values gets pulled in till the array is completed.
Can somebody change and tell me. Pardon me for asking code change as I don't know PHP.
Please do let me know how do I change it so that db1 or some other value randomly don't get pulled in till it becomes unique.
I give you a germ of a solution,
This ensures that the previous selected value will NOT be the same as the currently selected value
For the complete solution you will need to write the values you read ($user_a) into another array and then you must
1 grab the first value
2 loop through the array and make sure that it doesn't match any other values
3.grab the second value
4. starting at element 1 loop through the array and make sure that it doesn't match any other element
NOTE make sure you don't compare any element with itself otherwise it will give a false positive
$users_a = array("db1", "db1p1", "db1p2", "db1p3", "db1p4", "db1p5", "db1p6", "db1p7", "db1p8", "db1p9", "db1p10", "db1p11", "db1p12", "db1p13", "db1p14", "db1p15", "db12", "db1p16", "db1p17", "db1p18", "db1p19", "db1p20");
$PreviousUser_a;//Keep track of what the previous $user_a variable contained
$Continue=TRUE;//Contune with the loop
while($Continue==TRUE)//Will loop until we pick a different value from last time
$user_a = $users_a[array_rand($users_a)];
if($user_a!=$this->PreviousUser_a) //We have NOT picked the same value twice in a row so we will exit this loop
$this->PreviousUser_a=$user_a;//Keep track of what we picked this time to see that we dont pick the same value next time