•

Check out our Community Blogs Register and join over 40,000 other developers!

### Popular Tags      # Displaying a random string

string

### #1 RHochstenbach

RHochstenbach

CC Resident

•    • 56 posts

Posted 14 March 2012 - 02:23 PM

The following tutorial is useful to display random strings. Why would you want to do such a thing? One of the uses is displaying a Message of the Day. In this example I will display a random letter from the alphabet.

1. Define an array.
```\$my_array = array();
```

2. Create a key for each string. You don't have to specify an index number (the number inside the brackets [ ] ) for each key, but I am doing so to make the flow better understandable.
```
\$my_array = "A";
\$my_array = "B";
\$my_array = "C";
\$my_array = "D";
\$my_array = "E";
\$my_array = "F";
\$my_array = "G";
\$my_array = "H";
\$my_array = "I";
\$my_array = "J";
\$my_array = "K";
\$my_array = "L";
\$my_array = "M";
\$my_array = "N";
\$my_array = "O";
\$my_array = "P";
\$my_array = "Q";
\$my_array = "R";
\$my_array = "S";
\$my_array = "T";
\$my_array = "U";
\$my_array = "V";
\$my_array = "W";
\$my_array = "X";
\$my_array = "Y";
\$my_array = "Z";

```

3. We need to store the highest index number (in this case '25') in a variable. This array has 26 items, but the highest index number is '25' because arrays start at '0'. So we count the size of the array using count(\$my_array), and then subtract '1' from it.
```\$array_size = count(\$my_array) - 1;
```

4. Now generate a random number between '0' (the lowest number in the array) and the value you've stored in the variable above. This will generate a random index number that matches an index number inside your array.
```\$random_index = mt_rand(0,\$array_size);
```

5. Print the random key by putting the generated number above inside the brackets.
```
echo \$my_array[\$random_index];

```

The complete code:
```
// An array with some text.
// Modify it to contain the random texts it will show.
\$my_array = array();
\$my_array = "A";
\$my_array = "B";
\$my_array = "C";
\$my_array = "D";
\$my_array = "E";
\$my_array = "F";
\$my_array = "G";
\$my_array = "H";
\$my_array = "I";
\$my_array = "J";
\$my_array = "K";
\$my_array = "L";
\$my_array = "M";
\$my_array = "N";
\$my_array = "O";
\$my_array = "P";
\$my_array = "Q";
\$my_array = "R";
\$my_array = "S";
\$my_array = "T";
\$my_array = "U";
\$my_array = "V";
\$my_array = "W";
\$my_array = "X";
\$my_array = "Y";
\$my_array = "Z";

// Get the size of the array for getting the highest index number.
// Arrays start at '0', so we subtract 1 from the total amount.
// Key number '26' will have an index of '25'
\$array_size = count(\$my_array) - 1;

// Generate a number between 0 and the highest index number of the array
\$random_index = mt_rand(0,\$array_size);

// Print the array key with the generated index number
echo \$my_array[\$random_index];

```

• 0

### #2 surajkumardotin

surajkumardotin
• Location:Ludhiana
• Programming Language:PHP, JavaScript
• Learning:C, C++, PHP, JavaScript

Posted 18 August 2015 - 11:58 PM

Why are not using array rand() function.

```<?php
echo rand('10','100');
?>
```

Edited by surajkumardotin, 18 August 2015 - 11:59 PM.

• 0

### Also tagged with one or more of these keywords: string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download 