Problem with random array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • noone

    #1

    Problem with random array

    hello all.
    I am using this code to randomly select one value from an array.

    srand ();
    $rec = array("1","2"," 3","4","5","6") ;
    $rec = $rec[rand(0,count($r ec))];
    print $rec;

    problem is, is sometimes it returns nothing. can anyone see how
    to make this so it always selects something.

    thanks.


  • Sami Haaramo

    #2
    Re: Problem with random array

    noone wrote:[color=blue]
    > hello all.
    > I am using this code to randomly select one value from an array.
    >
    > srand ();
    > $rec = array("1","2"," 3","4","5","6") ;
    > $rec = $rec[rand(0,count($r ec))];
    > print $rec;
    >
    > problem is, is sometimes it returns nothing. can anyone see how
    > to make this so it always selects something.
    >
    > thanks.[/color]

    Try $rec = $rec[rand(0,count($r ec)-1)];

    -Sami

    Comment

    • Tyrone Slothrop

      #3
      Re: Problem with random array

      On Fri, 27 Jun 2003 16:25:52 -0800, "noone" <noone@nowhere. com> wrote:
      [color=blue]
      >hello all.
      >I am using this code to randomly select one value from an array.
      >
      >srand ();
      >$rec = array("1","2"," 3","4","5","6") ;
      >$rec = $rec[rand(0,count($r ec))];
      >print $rec;
      >
      >problem is, is sometimes it returns nothing. can anyone see how
      >to make this so it always selects something.
      >
      >thanks.[/color]

      How about a do... while statement?

      do {
      $rec = $rec[rand(0,count($r ec))];
      } while ($rec = "");

      Comment

      • KIPAX

        #4
        Re: Problem with random array

        On Fri, 27 Jun 2003 16:25:52 -0800, "noone" <noone@nowhere. com> wrote:
        [color=blue]
        >hello all.
        >I am using this code to randomly select one value from an array.
        >
        >srand ();
        >$rec = array("1","2"," 3","4","5","6") ;
        >$rec = $rec[rand(0,count($r ec))];
        >print $rec;
        >
        >problem is, is sometimes it returns nothing. can anyone see how
        >to make this so it always selects something.[/color]


        count($rec) returns 6 because there are 6 elements

        rand(0,count($r ec)) starts from 0 to count($rec) ie 6
        0,1,2,3,4,5,6 is 7 elements.. and you only have 6 :)




        --
        KIPAX (www.kipax.com)

        Comment

        • Nobody

          #5
          Re: Problem with random array

          Actually, the problem is that you are asking for a random number between 0
          and 6. That's 7 possibilities. So one in seven times will reference an
          array element that does not exist.

          $rec = $rec[rand(0,count($r ec))];

          should be:

          $rec = $rec[rand(0,count($r ec)-1)];

          Jerry




          "Tyrone Slothrop" <ts@paranoids.c om> wrote in message
          news:fkopfv03c8 tqbpntco2uoc96h 8003usajl@4ax.c om...[color=blue]
          > On Fri, 27 Jun 2003 16:25:52 -0800, "noone" <noone@nowhere. com> wrote:
          >[color=green]
          > >hello all.
          > >I am using this code to randomly select one value from an array.
          > >
          > >srand ();
          > >$rec = array("1","2"," 3","4","5","6") ;
          > >$rec = $rec[rand(0,count($r ec))];
          > >print $rec;
          > >
          > >problem is, is sometimes it returns nothing. can anyone see how
          > >to make this so it always selects something.
          > >
          > >thanks.[/color]
          >
          > How about a do... while statement?
          >
          > do {
          > $rec = $rec[rand(0,count($r ec))];
          > } while ($rec = "");
          >[/color]


          Comment

          Working...