PHP: random numbers and random selection of text

Random selection of number
From a range
From a serie of numbers

 Random numbers I A randon selection within a range

We will obtain a random number between a minimum and a maximum value.

 1 2 3 4 5 6 7 8 9 10

Lines 3 and 4: the minimum and the maximum are set up.

Line 6: the random number is selected by using mt_rand () command . Alternatively, rand () command may be used, but mt_rand () command is superior.
The random number is stored in variable \$number.

Line 8: \$number is outputted.

 Random numbers II A randon selection of a number from a serie of numbers.

We will select one number from an array which contains several numbers (independent numbers, not related).

 1 2 3 4 5 6 7 8 9

Line 2: the number (options) for our selection are included in an array.

Line 5: The order of the elements contained in the array are randomly mixed, so a new order is obtained.

Line 7: The first number will be outputted (the randomly selected one).

 Random selection of text

The links will be contained into an array, and one of them will be selected.

 Option 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 link 1"; \$text="link 2"; \$text="link 3"; \$text="link 4"; \$text="link 5"; \$text="link 6"; \$n=sizeof(\$text)-1; \$number = mt_rand (0, \$n ); print \$text[\$number]; ?>

Lines 3 to 8: an array is defined, where each value of the array is text.

Line 10:  number of elements in the array are obtained.

Line 12:  a random number between 0 and number of texts is selected

Line 14:  By using the selected number, the corresponding text is outputted. .

Lines 3 to 8: an array is defined, where each value of the array is text.

Line 10:   The order of the elements contained in the array \$text are randomly mixed, so a new order is obtained.

Line 12:  The first text is outputted (being the first is a randon selection).

The basic idea is very simple:
• a string variable contains all the posible characters we may use to obtain the random password/text.
• a  number  between  0 and the length of the string will be randomly selected. The number is used to select one character from the string.
• the process is repeated as many times as the length of the password we want to obtain.
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Line 3: The length of the password is defined (a 10 characters length password in this example)

Line 5: \$str is the variable containing all the characters allowed in the password.

Line 6: \$max is basically the length of \$str (1 is substracted to avoid problems latter).

Line 8: the variable \$password is set up.

Lines 9-12: The password weill be selected in those lines. The cicle is repited up to the maximum length of the password.
Line 10: a random number is selected between o and thye length of %str.
Line 11: one character from variable \$str is selected by using command substr() and added to variable \$password.

Line 14: The password is outputted.