mt_rand


URL apg

GENERATE an integer  RANDOM VALUE  via MTRNG, Mersenne Twister Random Number Generator.





RANDOM NUMBERS

are numbers that occur in a sequence such that two conditions are met:

1 . the values are uniformly distributed over a defined interval or set.
2 . it is impossible to predict future values based on past or present ones.

Random numbers are important in statistical analysis and probability theory.

This function does not generate cryptographically secure values,therefore it should not be used for these purposes.

There are other functions in PHP that generate random integers for use in cryptography; such functions will be studied in the future.

 mt_rand  should be used instead of  rand  because it is much faster.

This function uses the MTRNG, Mersenne Twister Random Number:

   Mersenne Twister  !



<?php

// FIRST DESCRIPTION 

int mt_rand ( );
            
?>    

<?php

// SECOND DESCRIPTION 
 
int mt_rand (int $min, int $max );


where,


$min = the minimum integer value
                          of the numeric interval

$max 
= the maximun integer value
                          of the numeric interval obtained by
: 
                                       
mt_getrandmax = 2147483647
                       
?>

$min


The minimum integer value of the numeric interval.



$max


The maximum integer value of the numeric interval.




 SOLVE ALL PROPOSED EXERCISES 


             NOT SOLVED YET             


If possible in several versions of PHP


Especially the latest 7 and 8


Study the displayed data carefully


  1 EXERCISE   

<?php
 
$mtrd01  
= mt_rand();

echo 
'The generated random number 
                   for this system in this moment, is: ' 
. 
                                            
$mtrd01 . '.<br><br>';

?> 

  2 EXERCISE   

<?php

$min02 
= 10;

$max02 = 20;
 
$mtrd02  = mt_rand($min02, $max02);

echo 
'The random number generated for this system, 
                   within the numerical range:<br>[' 
. 
                         
$min02 . ', ' . $max02 . '], at this point is ' . 
                                                    
$mtrd02 . '.<br><br>'

?> 

  3 EXERCISE   

<?php

$min03 
= 0;

$max03 = mt_getrandmax();
 
$mtrd03  = mt_rand($min03, $max03);

echo 
'The random number generated for this system, 
             within the numerical range:<br>[' 
. 
                  
$min03 . ', ' . $max03 . '], at this point is ' . 
                                     
$mtrd03 . '.<br><br>';

?> 

  4 EXERCISE   

<?php

$min04 
= PHP_INT_MIN;
// -9223372036854775808 -> 
// -> Since PHP 7.0.0 (64bit)

$max04 = PHP_INT_MAX;
// 9223372036854775807  ->
// -> Since PHP 5.0.5 (64bit)
 
$mtrd04  = mt_rand($min04, $max04);

echo 
'The random number generated for this system, 
              within the numerical range:<br>[' 
. 
                    
$min04 . ', ' . $max04 . '], at this point is ' . 
                                      
$mtrd04 . '.<br><br>';

?> 

  5 EXERCISE   

<?php

$min05 
= (PHP_INT_MIN - 1);
// -9223372036854775809 

$max05 = (PHP_INT_MAX + 1);
// 9223372036854775808  
 
$rd05  = mt_rand($min05, $max05);

$min05a = (PHP_INT_MIN);
// -9223372036854775808 -> 
// -> Since PHP 7.0.0 (64bit)

$max05a = (PHP_INT_MAX);
// 9223372036854775807 -> 
// -> Since PHP 5.0.5 (64bit)

$rd05a  = mt_rand($min05a, $max05a);

echo 
'The random number generated for this system, 
              within the numerical range:<br>[' 
. 
                    
$min05a . ', ' . $max05a . '], at this point is ' . 
                                            
$rd05a . '.<br><br>';

?>  

 RESULT   

PHP 7.4.XX ( 64bit systems )

Warning: ...

The random number generated for this system, within the numerical range:

[-9223372036854775808, 9223372036854775807]

at this moment, the value is:
-4862817445697834864.

This is a particular result.
For each new run a new value will be generated.



PHP 8.0.XX ( 64bit systems )

Fatal error: ...