Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Need Random unique variable value in PHP 5

 
Ranch Hand
Posts: 256
Netbeans IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Existing:


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.
hxxp://www.php.net/manual/en/function.array-flip.php

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.
 
Ranch Hand
Posts: 52
1
MySQL Database Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
loop
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=array_flip($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
{$Continue=FALSE}
$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
 
    Bookmark Topic Watch Topic
  • New Topic