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

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

davidjwest wrote: 2. I don't understand "static", (see I did RTFM!)
[php_man]variables.scope[/php_man]
davidjwest wrote: 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?
Well, i use a loop to generate the values from 1 to 100, and i store them in an array.

Next i generate 1 random number, as an example how you could use it.
Offcourse you could write your own loop to generate more numbers ;)
davidjwest wrote: 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?
It executes once because, you only call getLut() once.
Once you have the array that maps number (index of the array) to the given value you can reuse that array.

To generate 10 thingies you want:

Code: Select all

$lut = getLut();
for ($i = 0; $i < 10; ++$i)
{
  echo $lut[rand(1,100)];
}
hth ;)
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

this should help you understand for loops

http://php.net/for

btw- if your new to php, your gonna be rtfm'n a lot.

heres a cool bookmarklet

make a bookmark, and use this for the location:

Code: Select all

javascript:q=document.getSelection();if(!q)void(q=prompt('PHP Reference:',''));if(q)location.href='http://www.php.net/'+escape(q)
try it
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Random

Post by Roja »

Weirdan wrote: ... unless you have better PRNG (or even real RNG) implemented as stdlib's random function. ;)
Background plays a part here. All the apps I develop generally are deployed by *other people* - so for me, portability trumps special-environment 6 days a week and twice on sundays. (Clearly, your mileage may vary).
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Random

Post by Weirdan »

Roja wrote: Background plays a part here. All the apps I develop generally are deployed by *other people*
Apparently you've met your own antipode in me :) Generally I develop app for one certain purpose, to be run in one specific environment (controlled by me). I deploy my apps and mantain it during their entire life-cycle.

Hope together we'll bring balance to these forums :)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Random

Post by Roja »

Weirdan wrote:
Roja wrote: Background plays a part here. All the apps I develop generally are deployed by *other people*
Apparently you've met your own antipode in me :) Generally I develop app for one certain purpose, to be run in one specific environment (controlled by me). I deploy my apps and mantain it during their entire life-cycle.

Hope together we'll bring balance to these forums :)
Heh! Very cool!
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post by davidjwest »

Thanks - again!

:D
Post Reply