本文整理汇总了PHP中random_int函数的典型用法代码示例。如果您正苦于以下问题:PHP random_int函数的具体用法?PHP random_int怎么用?PHP random_int使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了random_int函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(Eggdrop::class, 200)->create()->each(function ($egg) {
$egg->servers()->attach(random_int(1, 2));
$egg->servers()->attach(3);
});
}
示例2: testBirthday
/**
* Birthday spacings: Choose random points on a large interval.
* The spacings between the points should be asymptotically exponentially
* distributed.
*/
public function testBirthday()
{
// Number of buckets to make
$num_buckets = 17;
// How much tolerance should we allow? 0.01 = 1% 0.50 = 50%, etc.
$tolerance = 0.03;
$rand_min = 200000;
$rand_max = 600000;
$rand_step = 100000;
$minT = 1.0 - $tolerance;
$maxT = 1.0 + $tolerance;
for ($nums_to_generate = $rand_min; $nums_to_generate < $rand_max; $nums_to_generate += $rand_step) {
$buckets = array_fill(0, $num_buckets, 0);
// The number of ints we expect per bucket +/- 2%;
$min = (int) ceil($minT * $nums_to_generate / $num_buckets);
$max = (int) floor($maxT * $nums_to_generate / $num_buckets);
for ($i = 0; $i < $nums_to_generate; ++$i) {
$random = random_int(0, 999);
$bucket = $random % $num_buckets;
$buckets[$bucket]++;
}
for ($i = 0; $i < $num_buckets; ++$i) {
// Debugging code:
if ($buckets[$i] <= $min) {
var_dump(['bucket' => $i, 'value' => $buckets[$i], 'min' => $min, 'nums' => $nums_to_generate, 'reason' => 'below min']);
}
if ($buckets[$i] >= $max) {
var_dump(['bucket' => $i, 'value' => $buckets[$i], 'maax' => $max, 'nums' => $nums_to_generate, 'reason' => 'above max']);
}
$this->assertTrue($buckets[$i] < $max && $buckets[$i] > $min);
}
}
}
示例3: testOutput
public function testOutput()
{
$half_neg_max = ~PHP_INT_MAX / 2;
$integers = array(random_int(0, 1000), random_int(1001, 2000), random_int(-100, -10), random_int(-1000, 1000), random_int(~PHP_INT_MAX, PHP_INT_MAX), random_int("0", "1"), random_int(0.11111, 0.99999), random_int($half_neg_max, PHP_INT_MAX), random_int(0.0, 255.0), random_int(-4.5, -4.5));
$this->assertFalse($integers[0] === $integers[1]);
$this->assertTrue($integers[0] >= 0 && $integers[0] <= 1000);
$this->assertTrue($integers[1] >= 1001 && $integers[1] <= 2000);
$this->assertTrue($integers[2] >= -100 && $integers[2] <= -10);
$this->assertTrue($integers[3] >= -1000 && $integers[3] <= 1000);
$this->assertTrue($integers[4] >= ~PHP_INT_MAX && $integers[4] <= PHP_INT_MAX);
$this->assertTrue($integers[5] >= 0 && $integers[5] <= 1);
$this->assertTrue($integers[6] === 0);
$this->assertTrue($integers[7] >= $half_neg_max && $integers[7] <= PHP_INT_MAX);
$this->assertTrue($integers[8] >= 0 && $integers[8] <= 255);
$this->assertTrue($integers[9] === -4);
try {
$i = random_int("9223372036854775808", "9223372036854775807");
$this->assertFalse(is_int($i));
$i = random_int("-9223372036854775808", "9223372036854775807");
$this->assertFalse(true);
} catch (Error $ex) {
$this->assertTrue($ex instanceof Error);
} catch (Exception $ex) {
$this->assertTrue($ex instanceof Exception);
}
}
示例4: add_message
public function add_message(Request $request)
{
$item = new Item();
$item->id = random_int(0, 1000);
$item->text = $request->get('message', 'you send empty text....');
Event::fire(new ItemCreated($item));
}
示例5: get_new_salts
public static function get_new_salts()
{
// From wp-admin/setup-config.php in WordPress 4.5.
// Generate keys and salts using secure CSPRNG; fallback to API if enabled; further fallback to original wp_generate_password().
try {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$max = strlen($chars) - 1;
for ($i = 0; $i < 8; $i++) {
$key = '';
for ($j = 0; $j < 64; $j++) {
$key .= substr($chars, random_int(0, $max), 1);
}
$secret_keys[] = $key;
}
} catch (Exception $ex) {
$secret_keys = wp_remote_get('https://api.wordpress.org/secret-key/1.1/salt/');
if (is_wp_error($secret_keys)) {
$secret_keys = array();
for ($i = 0; $i < 8; $i++) {
$secret_keys[] = wp_generate_password(64, true, true);
}
} else {
$secret_keys = explode("\n", wp_remote_retrieve_body($secret_keys));
foreach ($secret_keys as $k => $v) {
$secret_keys[$k] = substr($v, 28, 64);
}
}
}
return $secret_keys;
}
示例6: generate_captcha
public static function generate_captcha() : string
{
$num1 = random_int(self::CAPTCHA_NUMBER_MIN, self::CAPTCHA_NUMBER_MAX);
$num2 = random_int(self::CAPTCHA_NUMBER_MIN, self::CAPTCHA_NUMBER_MAX);
switch (random_int(0, 3)) {
case 0:
$max = max($num1, $num2);
$min = min($num1, $num2);
$answer = $max - $min;
$question = "{$max} - {$min} =";
break;
case 1:
$answer = $num1;
$squared = $num1 * $num1;
$question = "√{$squared} =";
break;
case 2:
$answer = $num1 * $num2;
$question = "{$num1} × {$num2} =";
break;
default:
$answer = $num1 + $num2;
$question = "{$num1} + {$num2} =";
break;
}
$_SESSION[self::SESSION_CAPTCHA_ANSWER_KEY] = $answer;
return $question;
}
示例7: getTest
public function getTest()
{
$numberInitial = 200;
$numberMarried = floor($numberInitial * 10 / 100);
$genders = [Personnage::GENDER_FEMALE, Personnage::GENDER_MALE];
$chars = new Collection();
for ($i = 0; $i < $numberInitial; $i++) {
$char = new Personnage();
$chars->push($char);
$char->setGender($genders[array_rand($genders)]);
$char->setAge(random_int(1, 60));
$char->setName($i);
}
//Create some marriages
foreach ($chars as $char) {
if ($char->age > 15) {
$numberMarried--;
$spouse = new Personnage();
$spouse->setAge(max(15, random_int($char->age - 5, $char->age + 5)));
$spouse->setGender($char->gender == Personnage::GENDER_MALE ? Personnage::GENDER_FEMALE : Personnage::GENDER_MALE);
$spouse->setName("Spouse {$numberMarried}");
$relation = new MarriedTo($spouse, $char);
$spouse->addRelation($relation);
$chars->push($spouse);
//Get them some babies!
$totalBabies = random_int(0, min(abs($char->age - $spouse->age), 5));
$siblings = [];
for ($i = 0; $i < $totalBabies; $i++) {
$child = new Personnage();
$child->setGender($genders[array_rand($genders)]);
$child->setName("Child {$numberMarried}.{$i}");
$relation1 = new ParentOf($char, $child);
$relation2 = new ParentOf($spouse, $child);
$char->addRelation($relation1);
$spouse->addRelation($relation2);
$chars->push($child);
foreach ($siblings as $sibling) {
$relation = new SiblingOf($sibling, $child);
$sibling->addRelation($relation);
}
$siblings[] = $child;
}
}
if ($numberMarried <= 0) {
break;
}
}
/*$man1 = new Personnage();
$woman1 = new Personnage();
$man1->setName('man1');
$woman1->setName('woman1');
$man1->setAge(random_int(20, 50));
$woman1->setAge(max(15,random_int($man1->age - 5, $man1->age + 5)));
$married = new MarriedTo($man1, $woman1);
$man1->addRelation($married);*/
echo implode('<br/>', $chars->toArray());
}
示例8: getID
/**
* @return string
*/
public function getID() : string
{
if ($this->routeID === null) {
$this->routeID = sprintf('%012x%04x%04x%012x', random_int(0, 0xffffffffffff), random_int(0, 0xfff) | 0x4000, random_int(0, 0x3fff) | 0x8000, random_int(0, 0xffffffffffff));
}
return $this->routeID;
}
示例9: __construct
function __construct($id)
{
$this->id = str_replace('-', '_', $id);
// necessary to ensure we don't have ID collisions in the SQL
$this->row_iterator = 0;
$this->random_int = random_int(1, 1000000);
}
示例10: testTrackedTimeGetsSummarizedCorrectlyForTask
public function testTrackedTimeGetsSummarizedCorrectlyForTask()
{
$timediff_seconds1 = random_int(10, 9999);
$now = date('Y-m-d H:i:s');
$start1 = new \DateTime($now);
$stop1 = new \DateTime($now);
$dateinterval1 = new \DateInterval("PT{$timediff_seconds1}S");
$stop1->add($dateinterval1);
factory(Tracking::class)->create(['user_id' => $this->user->id, 'task_id' => $this->task1->id, 'started_at' => $start1->format('Y-m-d H:i:s'), 'stopped_at' => $stop1->format('Y-m-d H:i:s'), 'active' => false]);
$timediff_seconds2 = random_int(10, 9999);
$yesterday = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('n'), date('j') - 1, date('Y')));
$start2 = new \DateTime($yesterday);
$stop2 = new \DateTime($yesterday);
$dateinterval2 = new \DateInterval("PT{$timediff_seconds2}S");
$stop2->add($dateinterval2);
factory(Tracking::class)->create(['user_id' => $this->user->id, 'task_id' => $this->task1->id, 'started_at' => $start2->format('Y-m-d H:i:s'), 'stopped_at' => $stop2->format('Y-m-d H:i:s'), 'active' => false]);
$timediff_seconds3 = random_int(10, 9999);
$twodaysbefore = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('n'), date('j') - 1, date('Y')));
$start3 = new \DateTime($twodaysbefore);
$stop3 = new \DateTime($twodaysbefore);
$dateinterval3 = new \DateInterval("PT{$timediff_seconds3}S");
$stop3->add($dateinterval3);
factory(Tracking::class)->create(['user_id' => $this->user->id, 'task_id' => $this->task1->id, 'started_at' => $start3->format('Y-m-d H:i:s'), 'stopped_at' => $stop3->format('Y-m-d H:i:s'), 'active' => false]);
$start_expected_calculation = new \DateTime($now);
$stop_expected_calculation = new \DateTime($now);
$stop_expected_calculation->add(new \DateInterval('PT' . ($timediff_seconds1 + $timediff_seconds2 + $timediff_seconds3) . 'S'));
$expected_dateinterval = $start_expected_calculation->diff($stop_expected_calculation);
$task = Task::where('id', 1);
$this->assertEquals($expected_dateinterval, $this->task1->getTrackedTime());
}
示例11: addOrderDetails
/**
* @param \Orm\Zed\Sales\Persistence\SpySalesOrder $salesOrderEntity
*
* @return void
*/
protected function addOrderDetails(SpySalesOrder $salesOrderEntity)
{
$salesOrderEntity->setOrderReference(random_int(0, 9999999));
$salesOrderEntity->setIsTest(true);
$salesOrderEntity->setSalutation(SpySalesOrderTableMap::COL_SALUTATION_MR);
$salesOrderEntity->setFirstName('FirstName');
$salesOrderEntity->setLastName('LastName');
}
示例12: roll
public function roll()
{
if (function_exists('random_int')) {
$this->result = random_int(1, $this->sides);
} else {
$this->result = mt_rand(1, $this->sides);
}
}
示例13: generate
public static function generate()
{
if (version_compare(PHP_VERSION, '7.0.0', '<')) {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
} else {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xfff) | 0x4000, random_int(0, 0x3fff) | 0x8000, random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xffff));
}
}
示例14: roll
/**
* @param int $numDice Number of D6's to roll
* @return int
*/
public function roll(int $numDice = 1)
{
$total = 0;
for ($i = 0; $i < $numDice; $i++) {
$total += random_int(1, $this->range);
}
return $total;
}
示例15: rollDie
function rollDie($sides)
{
if (function_exists('random_int')) {
return random_int(1, $sides);
} else {
return mt_rand(1, $sides);
}
}