Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Php Random Selector, whats the code
khalilov
post Jul 21 2008, 12:21 PM
Post #1


Member [ Level 2 ]
Group Icon

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?
Go to the top of the page
 
+Quote Post
faulty.lee
post Jul 21 2008, 07:03 PM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 479
Joined: 5-November 06
Member No.: 17,016



QUOTE(khalilov @ Jul 21 2008, 08:21 PM) *
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
Go to the top of the page
 
+Quote Post
Mike gambino
post Jul 25 2008, 03:30 AM
Post #3


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 15
Joined: 25-July 08
Member No.: 31,646



QUOTE(khalilov @ Jul 21 2008, 08:21 AM) *
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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Random Banners Rotation...(2)
  2. Php Random Text Generating(4)
  3. Php Reading And Writing To File(4)
  4. How To Use Cookie In Your Web Site ?(1)
  5. Php: Write Random Text As Image(3)
  6. Any Tool/software For Php Code Generation ?(3)
  7. What's Wrong With This Preg_replace Code?(2)
  8. [PHP + MySQL] Separating The Results By Pages(0)
  9. How Can I Write PHP Code By This Formmail Html(5)
  10. Script For Viewing A Random Image Needed(3)
  11. Code Snippets Repository(0)
  12. Random Name Generator(2)
  13. What's Wrong With My Php Webpage?(2)
  14. Random With Functions?(4)
  15. Dynamic Php Image And Better Php Code Question(10)
  1. Activation Code(7)


 



- Lo-Fi Version Time is now: 8th September 2008 - 03:58 AM