|
|
|
|
![]() ![]() |
Jul 21 2008, 12:21 PM
Post
#1
|
|
|
Member [ Level 2 ] Group: Members Posts: 56 Joined: 17-July 08 Member No.: 31,503 |
Is there a PHP script that randomly selects a string from a list?
example, the list is: 1-Pie 2-Balls 3-eggs The script would view a random word from those 3 every time i run it. Also is there a function that gives a random number between 0 and a number i select? |
|
|
|
Jul 21 2008, 07:03 PM
Post
#2
|
|
|
Premium Member Group: [HOSTED] Posts: 479 Joined: 5-November 06 Member No.: 17,016 |
Is there a PHP script that randomly selects a string from a list? example, the list is: 1-Pie 2-Balls 3-eggs The script would view a random word from those 3 every time i run it. Also is there a function that gives a random number between 0 and a number i select? Here is your random number generator -> http://my2.php.net/rand All you need is to call the rand() function. If you want to generate a random number between 0 to x, then just call CODE rand(0, x); As for your random string, all you need is to extend the rand function a little bit CODE $strCol = array("Pie", "Balls", "eggs"); $i = rand(0, count($strCol) -1); echo $strCol[$i]; This is how it works. First you create an array of string for your list. Then calling rand and passing it 0 as min, and max would be array count - 1. You need to minus 1 because index is zero based. After that just index your array using the random number you got. With this setup, you can easily add more string to your array and the rest is taken care of automatically |
|
|
|
Jul 25 2008, 03:30 AM
Post
#3
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 15 Joined: 25-July 08 Member No.: 31,646 |
Is there a PHP script that randomly selects a string from a list? example, the list is: 1-Pie 2-Balls 3-eggs The script would view a random word from those 3 every time i run it. Also is there a function that gives a random number between 0 and a number i select? wow that's cool. I would like that but on my game website would be a lot more advanced than that. I have a LONG code for that one but it is hard to type it down for on thing. right now I have to get my website fully done. I will let people look at my website when it is done though. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 8th September 2008 - 03:58 AM |