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.
The moose likes PHP and the fly likes Need Random unique variable value in PHP 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Languages » PHP
Bookmark "Need Random unique variable value in PHP 5" Watch "Need Random unique variable value in PHP 5" New topic

Need Random unique variable value in PHP 5

Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 255

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 was searching google and found below nice examples but not able to implement the same here in my code.

I am asking this so that once one value is used up, it should refrain from assigning the same value once again for minimum of one hour. Once one hour passed then it should be free to use.

Regards, Ricky
Oracle Weblogic 10g Certified Expert TechPaste.Com
Stephen Black
Ranch Hand

Joined: Aug 13, 2009
Posts: 52

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");
class RandomnessPickeness
$PreviousUser_a;//Keep track of what the previous $user_a variable contained
function GetUser()
$Continue=TRUE;//Contune with the loop
while($Continue==TRUE)//Will loop until we pick a different value from last time
$user_a= shuffle($users_a);
$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
return $user_a;
Happy PHPing
Have you checked out Aspose?
subject: Need Random unique variable value in PHP 5
It's not a secret anymore!