Quick Php Question

twoflowtwoflow Singing Drunk Join Date: 2002-11-01 Member: 1950Members, Constellation
<div class="IPBDescription">rand() not working?</div>Hello to anyone who knows basic PHP, please humour me. Was trying to get my magic signature to randomly select a background image by using the rand(int1, int2) function. In this case, I'd use rand(1, 2) to select either 'base_image1' or 'base_image2'. Now, I tried adding the rand() function to:

$config['image_base'] = 'base_image';

loads of times, adding it at the back, front, in a different function and then calling it ($rando = rand(1, 2); didn't help). In the end, I just removed the 'base_image' part and it worked ($config['image_base'] = rand(1, 2);), but the curiosity's killing me. How are you supposed to do it?

Comments

  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    I know nothing about php, but if its anything like C, then you need to call srand(unsigned int) to seed the generation.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin--_Creep_+Jan 20 2004, 03:37 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (_Creep_ @ Jan 20 2004, 03:37 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I know nothing about php, but if its anything like C, then you need to call srand(unsigned int) to seed the generation. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    It is, and you do.
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    <!--QuoteBegin--SoulSkorpion+Jan 20 2004, 03:15 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Jan 20 2004, 03:15 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin--_Creep_+Jan 20 2004, 03:37 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (_Creep_ @ Jan 20 2004, 03:37 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I know nothing about php, but if its anything like C, then you need to call srand(unsigned int) to seed the generation. <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    It is, and you do. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    Seeding random number generation is usually done using the time.

    The 'time' function can be used for this.
  • tankefugltankefugl One Script To Rule Them All... Trondheim, Norway Join Date: 2002-11-14 Member: 8641Members, Retired Developer, NS1 Playtester, Constellation, NS2 Playtester, Squad Five Blue
    <!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->int rand ( [int min, int max])

    Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically. <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    $config['image_base'] = 'base_image' . rand(1, 2);
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
  • twoflowtwoflow Singing Drunk Join Date: 2002-11-01 Member: 1950Members, Constellation
    You bloody star, cheers tanky.
  • tankefugltankefugl One Script To Rule Them All... Trondheim, Norway Join Date: 2002-11-14 Member: 8641Members, Retired Developer, NS1 Playtester, Constellation, NS2 Playtester, Squad Five Blue
    Glad I could help.
Sign In or Register to comment.