Forum Moderators: coopster

Message Too Old, No Replies

How to output 7 unique random numbers

         

iamPariah

11:42 pm on Aug 19, 2008 (gmt 0)

10+ Year Member



Hi. I'm having a little trouble, and I hope you can help. I found this thread [webmasterworld.com], but when I use the code jatar_k posted, too many numbers are returned (I modified to generate random numbers between 1,7, as below).


<?php
$myrandnum = array();
for ($i=0;$i<7;$i++) {
$mynum = rand(1,7);
if (!in_array($mynum,$myrandnum)) $myrandnum[] = $mynum;
else $i--;
echo ($mynum . ', ');
} ?>

What am I doing wrong?

Also, I'm trying to use the above to include the random numbers to call a PHP function--seven copies of the function, each using a different number. It almost works.

This code...


<?php
$myrandnum = array();
for ($i=0;$i<5;$i++) {
$mynum = 'pl_postlist("PostsLatestHOME_' . rand(1,7) .'")';
if (!in_array($mynum,$myrandnum)) $myrandnum[] = $mynum;
else $i--;
echo ($mynum . '/n');
} ?>

...returns this the following as rendered HTML instead of executing the PHP function...


("PostsLatestHOME_3")/npl_postlist("PostsLatestHOME_1")/npl_postlist("PostsLatestHOME_1")/npl_postlist("PostsLatestHOME_5")/npl_postlist("PostsLatestHOME_5")/npl_postlist("PostsLatestHOME_5")/npl_postlist("PostsLatestHOME_3")/npl_postlist("PostsLatestHOME_1")/npl_postlist("PostsLatestHOME_4")/npl_postlist("PostsLatestHOME_2")/n

And, again, it returns more than the desired 7 results.

Help, please?

[edited by: iamPariah at 11:44 pm (utc) on Aug. 19, 2008]

ag_47

2:55 am on Aug 20, 2008 (gmt 0)

10+ Year Member



Your code looks fine. The reason you see many copies of the same functions is because you echo is inside the loop. After all, you loop keeps repeating whenever there is a duplicate.. hence more than 7 loops/echos. Instead, delete the ' echo ($mynum . '/n'); ' and put this AFTER the loop:
' print_r($myrandnum); '.
You'll see the actual contents of the final array, which will be 1 .. 7

MattAU

6:28 am on Aug 20, 2008 (gmt 0)

10+ Year Member



There are easier ways to do what you'd like. Is the /n meant to be a newline? If so it needs to be \n inside double quotes "\n"

$myrandnum = range(1,7);
shuffle($myrandnum);
foreach($myrandnum as $mynum)
{
echo pl_postlist('PostsLatestHOME_'.$mynum). "\n";
}

iamPariah

4:20 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



Trying your code, ag_47, like this...


<?php
$myrandnum = array();
for ($i=0;$i<5;$i++) {
$mynum = 'pl_postlist("PostsLatestHOME_' . rand(1,7) .'")';
if (!in_array($mynum,$myrandnum)) $myrandnum[] = $mynum;
else $i--;
} ?>

<?php

print_r($myrandnum);

?>

...returns this in the rendered php/html....


Array ( [0] => pl_postlist("PostsLatestHOME_2") [1] => pl_postlist("PostsLatestHOME_3") [2] => pl_postlist("PostsLatestHOME_4") [3] => pl_postlist("PostsLatestHOME_1") [4] => pl_postlist("PostsLatestHOME_7") )

Did I do something wrong?

iamPariah

4:22 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



Thanks, MattAU, but when I try your code (below), nothing happens--no output in the page source. Did I make a mistake?


<?php
$myrandnum = range(1,7);
shuffle($myrandnum);
foreach($myrandnum as $mynum)
?>

<?php
{
echo pl_postlist('PostsLatestHOME_'.$mynum). "/n";
}
?>

ag_47

4:53 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



** ..returns this in the rendered php/html....
** Did I do something wrong?

No, that's just the contents of the array. As you can see it's filled with 7 functions in a random order.

**when I try your code (below), nothing happens--no output in the page source
**echo pl_postlist('PostsLatestHOME_'.$mynum)

Thats because you are outputting the results of the function pl_postlist();
If you want to see the actual name of the function being called change the echo to:
<?php
$myrandnum = range(1,7);
shuffle($myrandnum);
foreach($myrandnum as $mynum){
echo "pl_postlist('PostsLatestHOME_$mynum')";
}
?>

It realty looks like yo should read up on your php/coding

iamPariah

7:57 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



"It realty looks like yo should read up on your php/coding "

Heh. I'm trying.

Maybe I'm going about asking for help the wrong way. I've tried to make it work myself a few ways, and I couldn't do it. Maybe if I'm a little more explicit in my intended end result it would be easier to help me get there.

I have to execute the following function 7 unique times, replacing "N" with a numeral 1-7, so as to load the function with a differently numbered variable each time. And, I need the results to be randomized with each execution--thus, it might load 7,5,3,2,1,4,6 or it might load 1,2,3,4,5,6,7. (I really hope I'm making sense here.)


<?php
if( function_exists("pl_postlist") )
pl_postlist("PostsAuthorHOME_N");
?>

Any advice?

ag_47

9:58 pm on Aug 20, 2008 (gmt 0)

10+ Year Member



All you have to do is remove the echo, so instead of printing the results, the function gets called :)

$myrandnum = range(1,7); //fill array with 1 .. 7

shuffle($myrandnum); //randomize the order

foreach($myrandnum as $N){

//loop through the array and call the function 7 times in the random order
pl_postlist('PostsLatestHOME_'.$N);

}

iamPariah

4:21 pm on Aug 22, 2008 (gmt 0)

10+ Year Member



Thanks, ag_47. Your code works--of course--but it was the pl_postlist function that wasn't working for me. Apparently calling it inside another PHP function prevents it from loading; it gets executed too late, I suppose. <sigh>

Still, your code helped me with an alternative method for accomplishing the task. Thank you very much!