Microsecond Help - Beginner

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

    Microsecond Help - Beginner

    Hi,

    I'm am brand new to PHP and I'm getting up to speed (slowly) running
    through some example tutorials. But I am having trouble getting a For
    Loop to work.

    It is supposed to return the microscond from the server each time it
    loops, but it is returning the same second each time through. I'm
    assuming this is because "function make_seed()" is declared outside of
    the loop, but I'm stuck on how to rectify this.

    Below is my code, any help would be appreciated!

    Kevin

    ------------------------------------------
    <html>
    <?
    function make_seed()
    {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
    }
    srand(make_seed ());
    $randvalue = rand(1,100);

    ?>
    <ul>
    <?
    for ($i = 1; $i<10; $i++){ ?>
    <li><strong>< ?= $randvalue ?></strong></li>
    <?}?>
    </ul>
    </html>
  • Jerry Stuckle

    #2
    Re: Microsecond Help - Beginner

    Kevin Cloutier wrote:[color=blue]
    > Hi,
    >
    > I'm am brand new to PHP and I'm getting up to speed (slowly) running
    > through some example tutorials. But I am having trouble getting a For
    > Loop to work.
    >
    > It is supposed to return the microscond from the server each time it
    > loops, but it is returning the same second each time through. I'm
    > assuming this is because "function make_seed()" is declared outside of
    > the loop, but I'm stuck on how to rectify this.
    >
    > Below is my code, any help would be appreciated!
    >
    > Kevin
    >
    > ------------------------------------------
    > <html>
    > <?
    > function make_seed()
    > {
    > list($usec, $sec) = explode(' ', microtime());
    > return (float) $sec + ((float) $usec * 100000);
    > }
    > srand(make_seed ());
    > $randvalue = rand(1,100);
    >
    > ?>
    > <ul>
    > <?
    > for ($i = 1; $i<10; $i++){ ?>
    > <li><strong>< ?= $randvalue ?></strong></li>
    > <?}?>
    > </ul>
    > </html>[/color]

    Hi, Kevin,

    It's always returning the same value because you're not calling
    make_seed() in your for loop.

    Try this:

    <html>
    <?
    function make_seed()
    {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
    }

    ?>
    <ul>
    <?
    for ($i = 1; $i<10; $i++){
    srand(make_seed ()); // Call from within loop
    $randvalue = rand(1,100);
    ?>
    <li><strong>< ?= $randvalue ?></strong></li>
    <?}?>
    </ul>
    </html>

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Kevin Cloutier

      #3
      Re: Microsecond Help - Beginner

      Jerry Stuckle wrote:[color=blue]
      > Kevin Cloutier wrote:
      >[color=green]
      >> Hi,
      >>
      >> I'm am brand new to PHP and I'm getting up to speed (slowly) running
      >> through some example tutorials. But I am having trouble getting a For
      >> Loop to work.
      >>
      >> It is supposed to return the microscond from the server each time it
      >> loops, but it is returning the same second each time through. I'm
      >> assuming this is because "function make_seed()" is declared outside of
      >> the loop, but I'm stuck on how to rectify this.
      >>
      >> Below is my code, any help would be appreciated!
      >>
      >> Kevin
      >>
      >> ------------------------------------------
      >> <html>
      >> <? function make_seed()
      >> {
      >> list($usec, $sec) = explode(' ', microtime());
      >> return (float) $sec + ((float) $usec * 100000);
      >> }
      >> srand(make_seed ());
      >> $randvalue = rand(1,100);
      >>
      >> ?>
      >> <ul>
      >> <?
      >> for ($i = 1; $i<10; $i++){ ?>
      >> <li><strong>< ?= $randvalue ?></strong></li>
      >> <?}?>
      >> </ul>
      >> </html>[/color]
      >
      >
      > Hi, Kevin,
      >
      > It's always returning the same value because you're not calling
      > make_seed() in your for loop.
      >
      > Try this:
      >
      > <html>
      > <?
      > function make_seed()
      > {
      > list($usec, $sec) = explode(' ', microtime());
      > return (float) $sec + ((float) $usec * 100000);
      > }
      >
      > ?>
      > <ul>
      > <?
      > for ($i = 1; $i<10; $i++){
      > srand(make_seed ()); // Call from within loop
      > $randvalue = rand(1,100);
      > ?>
      > <li><strong>< ?= $randvalue ?></strong></li>
      > <?}?>
      > </ul>
      > </html>
      >[/color]
      Jerry,

      That was it, thanks for your help! I'm not sure why I didn't pick that
      up. It's never good to try and learn with examples that are flawed!

      Thanks again,

      Kevin

      Comment

      Working...