SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random generator problem

    Warning: rand() expects exactly 2 parameters, 1 given in /home/.luther/electricromance/ElectricRomance.ORG/namegen.php on line 4

    Warning: rand() expects exactly 2 parameters, 1 given in /home/.luther/electricromance/ElectricRomance.ORG/namegen.php on line 9
    PHP Code:
    <?php
    $randMsg1 
    = array("martini""electric""juicy""tainted""retro");
    $MsgNum1 count($randMsg1);
    $MsgGenNum1 floor(rand(MsgNum1));
    echo 
    $randMsg1[$msgGenNum1];

    $randMsg2 = array("masquerade""kiss""romance""poison""wind""couture""love""crash");
    $MsgNum2 count($randMsg2);
    $MsgGenNum2 floor(rand(MsgNum2));
    echo 
    $randMsg2[$msgGenNum2];
    ?>
    I thought this would be a fairly easy script to code, but I must be wrong. I'm trying to generate a two word phrase to aid in naming a website.

    My server is on PHP5.
    ~ Hannah

  2. #2
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Second parameter is required.
    Have a look at this
    rand()
    Barbara

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The rand() function takes two arguments, a minimum and maximum value. Since you are selecting a random array element, the minimum is 0. The maximum is one less than the number of elements in the array. It is an easy fix:

    Code:
    <?php
    $randMsg1 = array("martini", "electric", "juicy", "tainted", "retro");
    $MsgNum1 = count($randMsg1);
    $MsgGenNum1 = floor(rand(0, $MsgNum1 - 1));
    echo $randMsg1[$msgGenNum1];
    
    $randMsg2 = array("masquerade", "kiss", "romance", "poison", "wind", couture",  "love", "crash");
    $MsgNum2 = count($randMsg2);
    $MsgGenNum2 = floor(rand(0, $MsgNum2 - 1));
    echo $randMsg2[$msgGenNum2];
    ?>
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP Manual
    int rand ( [int min, int max] )

    If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).
    Code php:
    $MsgGenNum1 = floor(rand(0,$MsgNum1-1));
    Saul

  5. #5
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    ~ Hannah

  6. #6
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now it's not echoing anything:
    http://electricromance.org/namegen.php

    PHP Code:
    <?php
    $randMsg1 
    = array("martini""electric""juicy""tainted""retro""majestic");
    $MsgNum1 count($randMsg1);
    $MsgGenNum1 floor(rand(0$MsgNum1 1));
    echo 
    $randMsg1[$msgGenNum1];

    $randMsg2 = array("masquerade""kiss""romance""poison""wind""couture""love""crash""skies""sanity");
    $MsgNum2 count($randMsg2);
    $MsgGenNum2 floor(rand(0$MsgNum2 1));
    echo 
    $randMsg2[$msgGenNum2];
    ?>
    ~ Hannah

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $randMsg1 
    = array("martini""electric""juicy""tainted""retro");
    echo 
    $randMsg1[array_rand($randMsg1)];



    $randMsg2 = array("masquerade""kiss""romance""poison""wind""couture""love""crash");
    echo 
    $randMsg2[array_rand($randMsg2)];
    http://us2.php.net/array_rand
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it's working perfectly now!
    ~ Hannah


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •