Old 07-01-2005, 10:29   #1 (permalink)
cam
vague™
 
cam's Avatar
 
Join Date: Mar 2004
Location: Glasgow
Posts: 5,595
php random array sorting

Trying to sort an array pseudo-randomly by feeding a pseudo-random seed to a function.

Basically, need to "randomize" a result set but need to be able to reconstruct that result set in the same order sometime in the future.

Already know how to generate random seeds, any ideas as to the sorting with a seed?

---

just to clarify:

array ( 1, 2, 3, 4, 5 )

Bob visits the site and gets the result set: 3,4,1,2,5

next time bob visits he gets 3,4,1,2,5 again.
__________________

Last edited by i_am_cam : 07-01-2005 at 10:39.
  Reply With Quote
Old 10-01-2005, 05:39   #2 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,127
You'll be needing the srand function

like this
PHP Code:
<?php
function do_seed() {
    if (!(
$seed $_GET['seed'])) $seed 0;
    
srand($seed);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<? 
do_seed
();
$myarray = array (12345);
shuffle($myarray);
?>
<form action="<? print $_SERVER['PHP_SELF']; ?>" method="get">
    <label for="seed">Seed</label><br />
    <input type="text" name="seed" id="seed" value="<? print $_GET['seed'?>"/><br />
    <input type="submit" value="seed array"/>
</form>
<pre>
<?
print_r
($myarray);
?>
</pre>
</body>
</html>

See it here: http://www.gsgd.co.uk/xhtml/rand_array.php
  Reply With Quote
Old 10-01-2005, 06:00   #3 (permalink)
cam
vague™
 
cam's Avatar
 
Join Date: Mar 2004
Location: Glasgow
Posts: 5,595
Ahh, i'd taken the notes on that being deprecated as meaning it can no longer be used but of course it can. Cheers stickmus, that's awesome
__________________
  Reply With Quote
Old 10-01-2005, 06:23   #4 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,127
Not deprecated, you just don't need to use it unless you do, if you see what i mean.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8