Welcome Guest ( Log In | Register )




                Web Hosting

 
Reply to this topicNew Topic
Php Random Selector, whats the code
khalilov
post Jul 21 2008, 12:21 PM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 255
Joined: 17-July 08
From: Atlantis
Member No.: 31,503
myCENTs:73.77


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


Super Member
Group Icon

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


QUOTE(khalilov @ Jul 21 2008, 08:21 PM) [snapback]125994[/snapback]
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) [snapback]125994[/snapback]
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 topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   11 ViRuaL 2,360 10th December 2008 - 10:14 PM
Last post by: iG-nick
No New Posts   3 Sir Joe 2,023 2nd December 2008 - 03:14 AM
Last post by: iG-Ben and Hank
No New Posts 13 FirefoxRocks 462 24th November 2008 - 03:54 AM
Last post by: FirefoxRocks
No New Posts   7 eina 374 20th November 2008 - 11:30 PM
Last post by: tansqrx
No New Posts   4 Feelay 433 8th May 2008 - 10:20 AM
Last post by: Jared
No New Posts   6 sxyloverboy 2,501 7th May 2008 - 03:34 PM
Last post by: iGuest
No New Posts   6 soleimanian 2,251 2nd May 2008 - 12:36 PM
Last post by: iGuest
No New Posts   4 demolaynyc 1,027 25th March 2007 - 05:35 PM
Last post by: yordan
No New Posts   2 Vyoma 2,741 14th October 2006 - 07:19 AM
Last post by: Vyoma
No New Posts   3 pyost 927 28th August 2006 - 09:26 PM
Last post by: vhortex
No New Posts   2 laurenlovesyou 640 28th June 2006 - 03:21 PM
Last post by: austiniskoge
No New Posts   0 oval 1,418 3rd May 2006 - 08:34 PM
Last post by: oval
No New Posts   0 mark.smith.mvs 461 30th November 2005 - 03:19 PM
Last post by: mark.smith.mvs
No New Posts   3 Rejected 1,747 15th September 2005 - 06:25 AM
Last post by: jvizueta
No New Posts   1 amit 1,555 22nd August 2005 - 04:47 PM
Last post by: miCRoSCoPiC^eaRthLinG