Random

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Random

Post 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!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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];
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

timvw, you da man!

:P :P
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Random

Post 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!
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Loop

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Random

Post 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 :)
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post 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
!!!
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post by davidjwest »

Now that I understand, why does PHP have to be so complex!?

:wink: :D
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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').
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post 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!
Post Reply