Page 1 of 2

Random

Posted: Tue Nov 09, 2004 4:15 pm
by davidjwest
Hi, my first question on this forum and it's a pretty simple one I suspect!

I'm wanting to generate some random numbers for a game I am programming, from 1-100 inclusive. I then want to assign a text description to the number depending on what it is, for example 1-10 is going to be "suicidal" etc

I was thinking something like the below example would do the job, but it only shows how to assign variables if the random number is 1, 2 etc, not for a range of numbers. (It's at the bottom of the page URL below)

http://www.macdonald.egate.net/CompSci/PHP/random.html

Can you use the "case" command with a range?

I've checked on the PHP site, http://uk2.php.net/
and it doesn't even mention a case command.

Also there seems to be more than one command for generating random numbers, can someone enlighten me as to which I should use?

Thanks for wasting your time.

I hope this makes sense, if not I'll post some more waffle!

Posted: Tue Nov 09, 2004 4:33 pm
by tim
http://us2.php.net/manual/en/function.rand.php

pretty simple, then imply an if-else statements to assign your "titles" tot eh number the rand function generates

:D

Posted: Tue Nov 09, 2004 4:33 pm
by timvw
if you have to do it _many_ times, creating a lookup table might give you some performance boost

Code: Select all

function getLUT(){
   $values = array();
  for ($i = 0; $i < 101; ++$i){
    if ($i < 10){
      $values[$i] = 'WILLY';
    } elseif ($i < 50) {
       $values[$i] = 'JENNY';
     } else {
       $values[$i] = 'JOHN';
     }
   return $values;
}

$lut = getLUT();
$random = rand(0, 101);
echo $lut[$random];

Posted: Tue Nov 09, 2004 4:35 pm
by tim
timvw, you da man!

:P :P

Posted: Tue Nov 09, 2004 4:45 pm
by Weirdan
timvw wrote:if you have to do it _many_ times, creating a lookup table might give you some performance boost
and even more optimized version:

Code: Select all

function getLUT(){
  static $values = array();
  if($values) return $values;
  for ($i = 0; $i < 101; ++$i) {
    if ($i < 10) {
      $values[$i] = 'WILLY';
    } elseif ($i < 50) {
       $values[$i] = 'JENNY';
    } else {
       $values[$i] = 'JOHN';
    }
   }
   return $values;
}

$lut = getLUT();
$random = rand(0, 101);
echo $lut[$random]; 
// unfortunately php4 does not support this syntax:
// echo getLUT()[rand(0,101)];
8)

Re: Random

Posted: Tue Nov 09, 2004 4:52 pm
by Roja
davidjwest wrote: Also there seems to be more than one command for generating random numbers, can someone enlighten me as to which I should use?
Anytime you see "rand" and "srand", replace them with "mt_rand", and "mt_srand".

mt_rand is faster, more consistently random on multiple platforms, and has proven random characteristics - something that rand calls on specific platforms DONT have.

Plus, if you are on > php-4.2.0, seeding the value isnt even neccesary - it does it automatically. Bonus!

Posted: Tue Nov 09, 2004 4:53 pm
by davidjwest
Thanks very much guys.

I more or less understand what's going on (very new to PHP)

apart from

Code: Select all

for ($i = 0; $i < 101; ++$i)&#123;
What's this bit doing?

I'll be back later to ask more dumb questions no doubt!

Loop

Posted: Tue Nov 09, 2004 5:00 pm
by neophyte
That's a loop.
for ($i = 0; $i < 101; ++$i){
$i continues to increment by one until it hits 100 during which time the code below is executing.

Code: Select all

<?php
 if ($i < 10) {
      $values[$i] = 'WILLY';
    } elseif ($i < 50) {
       $values[$i] = 'JENNY';
    } else {
       $values[$i] = 'JOHN';
    }
   }
 ?>
The code above assigns the 'NAMES' to an array called values.

Re: Random

Posted: Tue Nov 09, 2004 5:23 pm
by Weirdan
Roja wrote: Anytime you see "rand" and "srand", replace them with "mt_rand", and "mt_srand"...
... unless you have better PRNG (or even real RNG) implemented as stdlib's random function. ;)
Roja wrote: mt_rand is faster, more consistently random on multiple platforms, and has proven random characteristics - something that rand calls on specific platforms DONT have.
marked as bold ;)
Roja wrote: Plus, if you are on > php-4.2.0, seeding the value isnt even neccesary - it does it automatically.
rand is auto-seeded as well :)

Posted: Wed Nov 10, 2004 9:41 am
by Jean-Yves
davidjwest wrote:Thanks very much guys.

I more or less understand what's going on (very new to PHP)

apart from

Code: Select all

for ($i = 0; $i < 101; ++$i)&#123;
What's this bit doing?
Hi David,

Good to see you took mine and Roope's advice and joined these excellent boards :)

As stated above, this code is a loop. In your favourite language, Sinclair ZX BASIC (;)), it's the equivalent of

Code: Select all

FOR i = 0 TO 100
!!!

Posted: Wed Nov 10, 2004 2:31 pm
by davidjwest
Now that I understand, why does PHP have to be so complex!?

:wink: :D

Posted: Wed Nov 10, 2004 3:32 pm
by davidjwest
I've just tried this code and I get an error:

Code: Select all

<?php
  function getLUT()&#123;
   $values = array();
  for ($i = 0; $i < 101; ++$i)&#123;
    if ($i < 10)&#123;
      $values&#1111;$i] = 'WILLY';
    &#125; elseif ($i < 50) &#123;
       $values&#1111;$i] = 'JENNY';
     &#125; else &#123;
       $values&#1111;$i] = 'JOHN';
     &#125;
   return $values;
&#125;

$lut = getLUT();
$random = rand(0, 101);
echo $lut&#1111;$random];

?>
The error is:

Parse error: parse error, unexpected $ in /home/qhwslos/public_html/random.php on line 20

Any clues where I went wrong, is there an extra bracket somewhere I don't need?

Go easy on me guys, I'm a noob!

:D

Posted: Wed Nov 10, 2004 3:37 pm
by Weirdan
there was missing closing curly bracket (}) before return statement. Either add it to the code you're using or use the function from my post ('even more optimized version').

Posted: Wed Nov 10, 2004 3:42 pm
by davidjwest
Weirdan wrote:there was missing closing curly bracket (}) before return statement. Either add it to the code you're using or use the function from my post ('even more optimized version').
LOL, thanks and will do!

I didn't choose your code as I don't understand it quite so well, I'll ask some more questions once I get it working.

:D

Posted: Wed Nov 10, 2004 4:04 pm
by davidjwest
Thanks, that worked!

I need to modify the code now to do exactly what I want so I need to understand it better, dumb question time!

Oh and I am sorry to ask this type of stuff, I really should RTFM but I'm terrible at remembering stuff unless I actually do it. This type of forum is great IMHO as I can learn in a more "interactive" way if you see what I mean. As long as you guys don't get too fed up with me then that's cool!

1. getLUT is just the function name you chose, it could be any name right?
2. I don't understand "static" it's not a command according to :http://uk2.php.net/manual-lookup.php?pattern=static

(see I did RTFM!)

3. You use a for loop in there but it only generates one random number, when I tried to change the < to a > it didn't execute at all, why use a loop? How would I get this to produce a load of random numbers?

4. I understand the next few lines
:D

5. I think I answered q3, the return function, I'm used to Basic so this is now confusing the heck out of me, for - return? I guess the whole bit of code is a subroutine so the return jumps out of it? Is that why it executes once?

6. You appear to generate the random number right at the end, which seems odd to me but I think I kind of understand, can you explain in simple words of no more than one syllable? :wink:

BTW I'm in no way questioning your programming ability here, just asking questions as I'm too new to PHP to fully get it,. Your talents are lost on one so numptie

:D

Thanks for the help so far though, it is really appreciated!