Jump to content
xisto Community
khalilov

Php Random Selector whats the code

Recommended Posts

Is there a PHP script that randomly selects a string from a list?example, the list is: 1-Pie 2-Balls 3-eggsThe 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?

Share this post


Link to post
Share on other sites

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://forums.xisto.com/no_longer_exists/

All you need is to call the rand() function. If you want to generate a random number between 0 to x, then just call
rand(0, x);

As for your random string, all you need is to extend the rand function a little bit
$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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.