note 118636 added to function.random-int

From: Date: Thu, 14 Jan 2016 00:30:39 +0000
Subject: note 118636 added to function.random-int
Groups: php.notes 
Request: Send a blank email to php-notes+get-204663@lists.php.net to get a copy of this message
This function is based on Andrew Moore's UUID generation function on the uniqid function; it has been updated to use random_int() on PHP 7.0 or later yet continue to function with earlier versions using mt_rand(). function generateUUIDv4() { if(version_compare(PHP_VERSION,'7.0.0', '<') ) { return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', // 32 bits for "time_low" mt_rand(0, 0xffff), mt_rand(0, 0xffff), // 16 bits for "time_mid" mt_rand(0, 0xffff), // 16 bits for "time_hi_and_version", // four most significant bits holds version number 4 mt_rand(0, 0x0fff) | 0x4000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 mt_rand(0, 0x3fff) | 0x8000, // 48 bits for "node" mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) ); } else { return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', // 32 bits for "time_low" random_int(0, 0xffff), random_int(0, 0xffff), // 16 bits for "time_mid" random_int(0, 0xffff), // 16 bits for "time_hi_and_version", // four most significant bits holds version number 4 random_int(0, 0x0fff) | 0x4000, // 16 bits, 8 bits for "clk_seq_hi_res", // 8 bits for "clk_seq_low", // two most significant bits holds zero and one for variant DCE1.1 random_int(0, 0x3fff) | 0x8000, // 48 bits for "node" random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xffff) ); } } ---- Server IP: 72.52.91.14 Probable Submitter: 2601:406:4d00:fefe:5568:a09f:c6b3:8f32 ---- Manual Page -- http://php.net/manual/en/function.random-int.php Edit -- https://master.php.net/note/edit/118636 Del: integrated -- https://master.php.net/note/delete/118636/integrated Del: useless -- https://master.php.net/note/delete/118636/useless Del: bad code -- https://master.php.net/note/delete/118636/bad+code Del: spam -- https://master.php.net/note/delete/118636/spam Del: non-english -- https://master.php.net/note/delete/118636/non-english Del: in docs -- https://master.php.net/note/delete/118636/in+docs Del: other reasons-- https://master.php.net/note/delete/118636 Reject -- https://master.php.net/note/reject/118636 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204663) next »