{"id":1,"date":"2019-08-28T19:04:10","date_gmt":"2019-08-28T19:04:10","guid":{"rendered":"https:\/\/tutorialio.com\/?p=1"},"modified":"2019-08-28T19:04:10","modified_gmt":"2019-08-28T19:04:10","slug":"random-number-functions-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/random-number-functions-in-php\/","title":{"rendered":"Random Number Functions in PHP"},"content":{"rendered":"<table class=\"quick-links\">\n<caption>Random Number Functions Functions in PHP<\/caption>\n<\/caption>\n<tbody>\n<tr>\n<th>Function Name<\/th>\n<th>Function Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"#getrandmax\">getrandmax()<\/a><\/td>\n<td>Show largest possible random value<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#mtgetrandmax\">mt_getrandmax()<\/a><\/td>\n<td>Show largest possible random value<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#mtrand\">mt_rand()<\/a><\/td>\n<td>Generate a random value via the Mersenne Twister Random Number Generator<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#mtsrand\">mt_srand()<\/a><\/td>\n<td>Seeds the Mersenne Twister Random Number Generator<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#rand\">rand()<\/a><\/td>\n<td>Generate a random integer<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#srand\">srand()<\/a><\/td>\n<td>Seed the random number generator<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"getrandmax\">PHP <code>getrandmax()<\/code> Function<\/h2>\n<h3>PHP <code>getrandmax()<\/code> Usage<\/h3>\n<p>The PHP <code><code>getrandmax()<\/code><\/code> function will give you the maximum value that can be returned by a call to <code>rand()<\/code>.<\/p>\n<h3>PHP <code>getrandmax()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> getrandmax ( void ) : int<\/code><\/pre>\n<h3>PHP <code>getrandmax()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This function does not accept any parameters.<\/p>\n<h3>PHP <code>getrandmax()<\/code> Return Value<\/h3>\n<p>The PHP <code>getrandmax()<\/code> function returns d by <code>rand()<\/code><\/p>\n<h2 id=\"mt_getrandmax\">PHP <code>mt_getrandmax()<\/code> Function<\/h2>\n<h3>PHP <code>mt_getrandmax()<\/code> Usage<\/h3>\n<p>The PHP <code><code>mt_getrandmax()<\/code><\/code> function will give you the maximum value that can be returned by a call to <code>mt_rand()<\/code>.<\/p>\n<h3>PHP <code>mt_getrandmax()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> mt_getrandmax ( void ) : int<\/code><\/pre>\n<h3>PHP <code>mt_getrandmax()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This function does not accept any parameters.<\/p>\n<h3>PHP <code>mt_getrandmax()<\/code> Return Value<\/h3>\n<p>The PHP <code>mt_getrandmax()<\/code> function returns the maximum random value returned by a call to <code>mt_rand()<\/code> without arguments, which is the maximum value that can be used for its <code class=\"parameter\">max<\/code> parameter without the result being scaled up (and therefore less random).<\/p>\n<h3>PHP <code>mt_getrandmax()<\/code> Working Examples<\/h3>\n<h4>1. Calculate a random floating-point number<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\nfunction randomFloat($min = 0, $max = 1) {\n    return $min + mt_rand() \/ mt_getrandmax() * ($max - $min);\n}\nvar_dump(randomFloat());\nvar_dump(randomFloat(2, 20));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">float(0.91601131712832)\nfloat(16.511210331931)<\/code><\/pre>\n<h2 id=\"mt_rand\">PHP <code>mt_rand()<\/code> Function<\/h2>\n<h3>PHP <code>mt_rand()<\/code> Usage<\/h3>\n<p>The PHP <code><code>mt_rand()<\/code><\/code> function will generate a random value via the Mersenne Twister Random Number Generator.<\/p>\n<h3>PHP <code>mt_rand()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> mt_rand ( void ) : int<\/code><\/pre>\n<pre><code class=\"lang-php\"> mt_rand ( int $min , int $max ) : int<\/code><\/pre>\n<h3>PHP <code>mt_rand()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>min<\/code> \u2014 Optional lowest value to be returned (default: 0) <\/p>\n<\/li>\n<li>\n<p><code>max<\/code> \u2014 Optional highest value to be returned (default: <code>mt_getrandmax()<\/code>) <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>mt_rand()<\/code> Return Value<\/h3>\n<p>The PHP <code>mt_rand()<\/code> function returns a random integer value between <code class=\"parameter\">min<\/code> (or 0) and <code class=\"parameter\">max<\/code> (or <code>mt_getrandmax()<\/code>, inclusive), or <code>FALSE<\/code> if <code class=\"parameter\">max<\/code> is less than <code class=\"parameter\">min<\/code>.<\/p>\n<h3>PHP <code>mt_rand()<\/code> Working Examples<\/h3>\n<h4>1. mt_rand() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho mt_rand() . \"\\n\";\necho mt_rand() . \"\\n\";\necho mt_rand(5, 15);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">1604716014\n1478613278\n6<\/code><\/pre>\n<h3>Changelog for PHP mt_rand()<\/code> Function<\/h3>\n<p><strong>7.2.0 \u2014 <\/strong> <code>mt_rand()<\/code> has received a bug fix for a modulo bias bug. This means that sequences generated with a specific seed may differ from PHP 7.1 on 64-bit machines. <\/p>\n<p><strong>7.1.0 \u2014 <\/strong> <code>rand()<\/code> has been made an alias of <code>mt_rand()<\/code>. <\/p>\n<p><strong>7.1.0 \u2014 <\/strong> <code>mt_rand()<\/code> has been updated to use the fixed, correct, version of the Mersenne Twister algorithm. To fall back to the old behaviour, use <code>mt_srand()<\/code> with <code>MT_RAND_PHP<\/code> as the second parameter. <\/p>\n<p><strong>5.3.4 \u2014 <\/strong> Issues an <code>E_WARNING<\/code> and returns <code>FALSE<\/code> if <code class=\"parameter\">max<\/code> &lt; <code class=\"parameter\">min<\/code>. <\/p>\n<h3>Important Points about PHP <code>mt_rand()<\/code> Function<\/h3>\n<ol>\n<li>\n<p>This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using <code>random_int()<\/code>, <code>random_bytes()<\/code>, or <code>openssl_random_pseudo_bytes()<\/code> instead.<\/p>\n<\/li>\n<li>\n<p> <code class=\"parameter\">min<\/code> <code class=\"parameter\">max<\/code> range must be within the range <code>mt_getrandmax()<\/code>. i.e. (<code class=\"parameter\">max<\/code> &#8211; <code class=\"parameter\">min<\/code>) &lt;= <code>mt_getrandmax()<\/code> Otherwise, <code>mt_rand()<\/code> may return poorer random numbers than it should. <\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: cyrax21<\/p>\n<pre><code>i wanted to spot out the big difference between rand and mt_rand when producing images using randomness as noise. \n \nfor example this is a comparation between rand and mt_rand on a 400x400 pixel png: http:\/\/oi43.tinypic.com\/vwtppl.jpg \n \ncode: \n<br>&lt;?php \nheader(\"Content-type: image\/png\"); \n$sizex=800; \n$sizey=400; \n \n$img = imagecreatetruecolor($sizex,$sizey); \n$ink = imagecolorallocate($img,255,255,255); \n \nfor($i=0;$i&lt;$sizex\/2;$i++) { \n  for($j=0;$j&lt;$sizey;$j++) { \n  imagesetpixel($img, rand(1,$sizex\/2), rand(1,$sizey), $ink); \n  } \n} \n \nfor($i=$sizex\/2;$i&lt;$sizex;$i++) { \n  for($j=0;$j&lt;$sizey;$j++) { \n  imagesetpixel($img, mt_rand($sizex\/2,$sizex), mt_rand(1,$sizey), $ink); \n  } \n} \n \nimagepng($img); \nimagedestroy($img); \n?&gt;<br> \n \nthe differences reduce when reducing the pixels of the image.. infact for a 100x100 pixel image the noise produced from the rand function is much more realistic than how it is for a 400x400 image: http:\/\/oi39.tinypic.com\/5k0row.jpg \n \n(rand is on the left, mt_rand on the right)<\/pre>\n<p><\/code><\/p>\n<h2 id=\"mt_srand\">PHP <code>mt_srand()<\/code> Function<\/h2>\n<h3>PHP <code>mt_srand()<\/code> Usage<\/h3>\n<p>The PHP <code><code>mt_srand()<\/code><\/code> function will seeds the Mersenne Twister Random Number Generator.<\/p>\n<h3>PHP <code>mt_srand()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> mt_srand ([ int $seed [, int $mode = MT_RAND_MT19937 ]] ) : void<\/code><\/pre>\n<h3>PHP <code>mt_srand()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>seed<\/code> \u2014 An arbitrary integer seed value. <\/p>\n<\/li>\n<li>\n<p><code>mode<\/code> \u2014 Use one of the following constants to specify the implementation of the algorithm to use. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>mt_srand()<\/code> Return Value<\/h3>\n<p>The PHP <code>mt_srand()<\/code> function returns d.<\/p>\n<h3>PHP <code>mt_srand()<\/code> Working Examples<\/h3>\n<h4>1. mt_srand() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n\/\/ seed with microseconds\nfunction make_seed()\n{\n  list($usec, $sec) = explode(' ', microtime());\n  return $sec + $usec * 1000000;\n}\nmt_srand(make_seed());\n$randval = mt_rand();\n?&gt;<\/code><\/pre>\n<h3>Changelog for PHP mt_srand()<\/code> Function<\/h3>\n<p><strong>7.1.0 \u2014 <\/strong> <code>srand()<\/code> has been made an alias of <code>mt_srand()<\/code>. <\/p>\n<p><strong>7.1.0 \u2014 <\/strong> <code>mt_rand()<\/code> has been updated to use the fixed, correct, version of the Mersenne Twister algorithm. To fall back to the old behaviour, use <code>mt_srand()<\/code> with <code>MT_RAND_PHP<\/code> as the second parameter. <\/p>\n<p><strong>5.2.1 \u2014 <\/strong> The Mersenne Twister implementation in PHP now uses a new seeding algorithm by Richard Wagner. Identical seeds no longer produce the same sequence of values they did in previous versions. This behavior is not expected to change again, but it is considered unsafe to rely upon it nonetheless. <\/p>\n<h2 id=\"rand\">PHP <code>rand()<\/code> Function<\/h2>\n<h3>PHP <code>rand()<\/code> Usage<\/h3>\n<p>The PHP <code><code>rand()<\/code><\/code> function will give you a pseudo-random integer between 0 and <code>getrandmax()<\/code>. If you want a random number between 5 and 15 (inclusive), for example, use <strong>rand(5, 15)<\/strong>.<\/p>\n<h3>PHP <code>rand()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> rand ( void ) : int<\/code><\/pre>\n<pre><code class=\"lang-php\"> rand ( int $min , int $max ) : int<\/code><\/pre>\n<h3>PHP <code>rand()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>min<\/code> \u2014 The lowest value to return (default: 0) <\/p>\n<\/li>\n<li>\n<p><code>max<\/code> \u2014 The highest value to return (default: <code>getrandmax()<\/code>) <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>rand()<\/code> Return Value<\/h3>\n<p>The PHP <code>rand()<\/code> function returns a pseudo random value between <code class=\"parameter\">min<\/code> (or 0) and <code class=\"parameter\">max<\/code> (or <code>getrandmax()<\/code>, inclusive).<\/p>\n<h3>PHP <code>rand()<\/code> Working Examples<\/h3>\n<h4>1. rand() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho rand() . \"\\n\";\necho rand() . \"\\n\";\necho rand(5, 15);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">7771\n22264\n11<\/code><\/pre>\n<h3>Changelog for PHP rand()<\/code> Function<\/h3>\n<p><strong>7.2.0 \u2014 <\/strong> <code>rand()<\/code> has received a bug fix for a modulo bias bug. This means that sequences generated with a specific seed may differ from PHP 7.1 on 64-bit machines. <\/p>\n<p><strong>7.1.0 \u2014 <\/strong> <code>rand()<\/code> has been made an alias of <code>mt_rand()<\/code>. <\/p>\n<h3>Important Points about PHP <code>rand()<\/code> Function<\/h3>\n<ol>\n<li>\n<p>This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using <code>random_int()<\/code>, <code>random_bytes()<\/code>, or <code>openssl_random_pseudo_bytes()<\/code> instead.<\/p>\n<\/li>\n<li>\n<p> <code class=\"parameter\">min<\/code> <code class=\"parameter\">max<\/code> range must be within the range <code>getrandmax()<\/code>. i.e. (<code class=\"parameter\">max<\/code> - <code class=\"parameter\">min<\/code>) &lt;= <code>getrandmax()<\/code> Otherwise, <code>rand()<\/code> may return poor-quality random numbers. <\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: Anonymous<\/p>\n<pre><code>quick way to generate randomish numbers and simple strings.\nno messing around with functions, so you can just pop the line into the middle of your existing code.\nnot the most perfect for sure, but ok for plenty of situations...\n<br>&lt;?php\n$random_number = intval( \"0\" . rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9) ); \/\/ random(ish) 5 digit int\n$random_string = chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)); \/\/ random(ish) 5 character string\n?&gt;<br>\nhope someone finds it useful for somthing.\nregards,\ndeeeeeen alxndr0u<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: phpdev<\/p>\n<pre><code>Here's an interesting note about the inferiority of the rand() function. Try, for example, the following code...\n<br>&lt;?php\n$r = array(0,0,0,0,0,0,0,0,0,0,0);\nfor ($i=0;$i&lt;1000000;$i++) {\n  $n = rand(0,100000);\n  if ($n&lt;=10) {\n    $r[$n]++;\n  }\n}\nprint_r($r); \n?&gt;<br>\nwhich produces something similar to the following output (on my windows box, where RAND_MAX is 32768):\nArray\n(\n    [0] =&gt; 31\n    [1] =&gt; 0\n    [2] =&gt; 0\n    [3] =&gt; 31\n    [4] =&gt; 0\n    [5] =&gt; 0\n    [6] =&gt; 30\n    [7] =&gt; 0\n    [8] =&gt; 0\n    [9] =&gt; 31\n    [10] =&gt; 0\n)\nWithin this range only multiples of 3 are being selected. Also note that values that are filled are always 30 or 31 (no other values! really!) \nNow replace rand() with mt_rand() and see the difference...\nArray\n(\n    [0] =&gt; 8\n    [1] =&gt; 8\n    [2] =&gt; 14\n    [3] =&gt; 16\n    [4] =&gt; 9\n    [5] =&gt; 11\n    [6] =&gt; 8\n    [7] =&gt; 9\n    [8] =&gt; 7\n    [9] =&gt; 7\n    [10] =&gt; 9\n)\nMuch more randomly distributed!\nConclusion: mt_rand() is not just faster, it is a far superior algorithm.<\/pre>\n<p><\/code><\/p>\n<h2 id=\"srand\">PHP <code>srand()<\/code> Function<\/h2>\n<h3>PHP <code>srand()<\/code> Usage<\/h3>\n<p>The PHP <code><code>srand()<\/code><\/code> function will seeds the random number generator with <code class=\"parameter\">seed<\/code> or with a random value if no <code class=\"parameter\">seed<\/code> is given.<\/p>\n<h3>PHP <code>srand()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> srand ([ int $seed ] ) : void<\/code><\/pre>\n<h3>PHP <code>srand()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>seed<\/code> \u2014 An arbitrary integer seed value. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>srand()<\/code> Return Value<\/h3>\n<p>The PHP <code>srand()<\/code> function returns d.<\/p>\n<h3>PHP <code>srand()<\/code> Working Examples<\/h3>\n<h4>1. srand() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n\/\/ seed with microseconds\nfunction make_seed()\n{\n  list($usec, $sec) = explode(' ', microtime());\n  return $sec + $usec * 1000000;\n}\nsrand(make_seed());\n$randval = rand();\n?&gt;<\/code><\/pre>\n<h3>Changelog for PHP srand()<\/code> Function<\/h3>\n<p><strong>7.1.0 \u2014 <\/strong> <code>srand()<\/code> has been made an alias of <code>mt_srand()<\/code>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Random Number Functions Functions in PHP Function Name Function Description getrandmax() Show largest possible random value mt_getrandmax() Show largest possible random value mt_rand() Generate a random value via the Mersenne Twister Random Number Generator mt_srand() Seeds the Mersenne Twister Random Number Generator rand() Generate a random integer srand() Seed the random number generator PHP getrandmax() [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[5,6],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-reference","tag-php","tag-random"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":0,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}