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

pretty simple, then imply an if-else statements to assign your "titles" tot eh number the rand function generates
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];and even more optimized version:timvw wrote:if you have to do it _many_ times, creating a lookup table might give you some performance boost
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)];Re: Random
Anytime you see "rand" and "srand", replace them with "mt_rand", and "mt_srand".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?
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
Thanks very much guys.
I more or less understand what's going on (very new to PHP)
apart from
What's this bit doing?
I'll be back later to ask more dumb questions no doubt!
I more or less understand what's going on (very new to PHP)
apart from
Code: Select all
for ($i = 0; $i < 101; ++$i){I'll be back later to ask more dumb questions no doubt!
Loop
That's a loop.
The code above assigns the 'NAMES' to an array called values.
$i continues to increment by one until it hits 100 during which time the code below is executing.for ($i = 0; $i < 101; ++$i){
Code: Select all
<?php
if ($i < 10) {
$values[$i] = 'WILLY';
} elseif ($i < 50) {
$values[$i] = 'JENNY';
} else {
$values[$i] = 'JOHN';
}
}
?>Re: Random
... unless you have better PRNG (or even real RNG) implemented as stdlib's random function.Roja wrote: Anytime you see "rand" and "srand", replace them with "mt_rand", and "mt_srand"...
marked as boldRoja 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.
rand is auto-seeded as wellRoja wrote: Plus, if you are on > php-4.2.0, seeding the value isnt even neccesary - it does it automatically.
Hi David,davidjwest wrote:Thanks very much guys.
I more or less understand what's going on (very new to PHP)
apart from
What's this bit doing?Code: Select all
for ($i = 0; $i < 101; ++$i){
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
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England
I've just tried this code and I get an error:
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!

Code: Select all
<?php
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];
?>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!
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England
LOL, thanks and will do!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').
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.
-
davidjwest
- Forum Commoner
- Posts: 67
- Joined: Sat Nov 06, 2004 5:26 am
- Location: Leeds, Yorkshire, England
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
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?
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
Thanks for the help so far though, it is really appreciated!
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
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?
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
Thanks for the help so far though, it is really appreciated!