Jump to content

[SOLVED] rand() Returning Priviously selected values.


centerwork

Recommended Posts

I am try to create a script that select a random entry in my DB and Then check a variable in that entry. If that varible is set the script will select a new random number.

 

The only probem is that it sometimes it selects the same random number as the last time it went through the loop.

 

This would be fine except Sometimes the condition is never met, and no entry is selected.

 

I also can't make the script run till one is select since it will be eventualy set to a cron job.

(So I limited the max number of times the script runs to the number of entries in my DB.)

 

Idealy a random function with a limiter something like this will be nice.

 

$excluding = '1,6,8,9';

$rand = rand(1,10,"$excluding");

 

Unfortunatly this does not seem to be an option with rand() from the manual.

 

What is the easyest way to do this?

 

 

Link to comment
Share on other sites

<?php
$rand2 = rand(1,3);
if ($rand2 == '1'){
$rand1 = rand(2,5);
}
elseif ($rand2 = '2'){
$rand1 = '7';
}
elseif ($rand2 = '3'){
$rand1 = '10';
}
echo $rand1;
//This will Exho a random number between 1-10 But wont allow the numbers 1,6,8,9
?>

 

This is the only way I can think of!

Link to comment
Share on other sites

The way i'd do it: Two tables in your database, one containing those numbers you don't want selected, and your table that you are trying to retrieve a random result from. Im suggeting using a table for the excluded numbers since i assume this will change.

 

So, we have the table `excluded` containing a single column `number`. We then use the query:

 

SELECT * FROM yourtable LEFT JOIN excluded ON excluded.number = yourtable.id WHERE excluded.number IS NULL ORDER BY RAND() LIMIT 1

 

Which selects one row, randomly ordered, where the id of our table is not in the excluded table.

 

You would then update the excluded table with the id of the row retrieved.

Link to comment
Share on other sites

try this

 

 

<?php
function urand($intMin,$intMax,$strExclude)
{
$intReturn = 0;
$intFound = 0;
$arrExclude = explode(",",$strExclude);

while ($intTempRand = rand($intMin,$intMax))
{
	if (!in_array($intTempRand,$arrExclude))
	{
		$intReturn = $intTempRand;
		$intFound=1;
		break;
	}
}
return $intReturn;
}

$excluding = '1,6,8,9';
$rand = urand(1,10,"$excluding");
echo $rand;

?>

 

 

Link to comment
Share on other sites

<?php
$array = array (
array (
number => "1"
),
array (
number => "2"
),
array (
number => "3"
),
array (
number => "4"
),
array (
number => "5"
),
array (
number => "7"
),
array (
number => "10"
)
);
$rand = rand(0,6);
while (is_array($array[$rand]['number'])){
echo $array[$rand]['number'];
?>

 

That should work using arrays?

Link to comment
Share on other sites

modification to my function that one was too messy  :P

 

<?php

function urand($intMin,$intMax,$strExclude)
{
$arrExclude = explode(",",$strExclude);

while ($intTempRand = rand($intMin,$intMax))
{
	if (!in_array($intTempRand,$arrExclude))
	{
		return  $intTempRand;
	}
}
}

$excluding = '1,6,8,9';
$rand = urand(1,10,"$excluding");
echo $rand;
?>

Link to comment
Share on other sites

I never thought to use the mysql query to pick the random entry.

 

<?php
// Connects to your Database Here

for($i=0; $i<=10; $i++)
{
$query = "SELECT * FROM visiter_print_log WHERE winner IS NULL ORDER BY RAND() LIMIT 1";
$results = mysql_query($query) or die (mysql_error());
$win_rows = mysql_num_rows($results);
if($win_rows == '0')
{
echo 'No Winner.';
}
$rows=mysql_fetch_array($results);
$winner = $rows['entry_id'];

//Picks a rand number that is not been selected aready.
echo 'Winner: '.$winner.'<br />';

$query = "UPDATE visiter_print_log SET visiter_print_log.winner='1' WHERE visiter_print_log.entry_id = '$winner'";
$results = mysql_query($query) or die (mysql_error());
echo 'Results: '.$results.'<br />';
}
?>

 

It worked GREAT... Thank you every one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.