Wednesday, June 6, 2012

PHP: random numbers and random selection of text


PHP: random numbers and random selection of text

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

Randon selection of text: links

Generate a random password


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
<?php

$min = 10000;
$max = 99999;

$number = mt_rand($min, $max);

print $number;

?>

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
<?php

$the_numbers = array(5,22,31,45,52,66,78);

shuffle ($the_numbers);


?>

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
<?

$text[0]="<a href=link1.html>link 1</a>";
$text[1]="<a href=link2.html>link 2</a>";
$text[2]="<a href=link3.html>link 3</a>";
$text[3]="<a href=link4.html>link 4</a>";
$text[4]="<a href=link5.html>link 5</a>";
$text[5]="<a href=link6.html>link 6</a>";

$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. .


Option 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?

$text[0]="<a href=link1.html>link 1</a>";
$text[1]="<a href=link2.html>link 2</a>";
$text[2]="<a href=link3.html>link 3</a>";
$text[3]="<a href=link4.html>link 4</a>";
$text[4]="<a href=link5.html>link 5</a>";
$text[5]="<a href=link6.html>link 6</a>";

shuffle ($text);

print $text[0];

?>

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).



Generate a random password

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
<?php

$passwordlength=10;

$str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ";
$max = strlen($str)-1;

$password="";
for ($i=0; $i<$passwordlength; $i++){
    $number = mt_rand(0,$max);
    $password.= substr($str,$number,1);
}

print $password;

?>

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.




No comments:

Post a Comment

There was an error in this gadget