Sorry, i copied a little extra stuff from console. This is what it is,
Anyways, it works fine if I put 0 in for 1 on line 9 where it uses the
rand(), but it include 0 and I can't have 0. Is there some way to fix that?
If you need to see the demo of it working with the 0 in place, i can do
that.

<?php

$s = array();
$n = 3;

function set_num() {
  global $s, $n;
  $add = "yes";
  $ran = rand(1, $n);
  if(count($s) > 0) {
    foreach($s as $sh) {
      if($ran == $sh) {
        $add = "no";
      }
    }
  }
  if($add == "yes") {
    $s[] = $ran;
  } else {
    set_num();
  }
}

while(count($s) <= $n) {
  set_num();
}
?>

----- Original Message ----- 
From: "CPT John W. Holmes" <[EMAIL PROTECTED]>
To: "Teren" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Friday, November 14, 2003 1:28 PM
Subject: Re: [PHP] Random Function


> You're code doesn't make sense. You're looking for 3 random numbers,
between
> 1 and 3 inclusive. Umm.. 1,2,3.
>
> Do you want them in a random order, or something?
>
> Maybe the upper limit of rand() and how many numbers you're looking for
> ($n), should be different??
>
> ---John Holmes...
>
> ----- Original Message ----- 
> From: "Teren" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, November 14, 2003 1:05 PM
> Subject: [PHP] Random Function
>
>
> Hi, I have the following function that generates a random number, but the
> key is it only generates the number once within the range you specify. So,
> if you want a range of 5 numbers, it will give you 5 different numbers. My
> problem is the function was set to start at 0, but I need it to start at
1,
> so i made a change to the function (line 9) and now the script only works
> every now and then. Can somebody look at it and see if they see why it
would
> be doing that? Thanks
>
> <?php
>
> $s = array();
> $n = 3;
>
> function set_num() {
>
> File random0.php saved.
> $ cat random0.php
> <?php
>
> $s = array();
> $n = 3;
>
> function set_num() {
>   global $s, $n;
>   $add = "yes";
>   $ran = rand(1, $n);
>   if(count($s) > 0) {
>     foreach($s as $sh) {
>       if($ran == $sh) {
>         $add = "no";
>       }
>     }
>   }
>   if($add == "yes") {
>     $s[] = $ran;
>   } else {
>     set_num();
>   }
> }
>
> while(count($s) <= $n) {
>   set_num();
> }
> ?>
>
> Teren
>
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to