我需要随机抽出数字×数字。
So lets say x is 5, I need a number to be eg. 35562 If x is 3, then it would throw back something like; 463
难道有人能向我表明如何做到这一点?
我需要随机抽出数字×数字。
So lets say x is 5, I need a number to be eg. 35562 If x is 3, then it would throw back something like; 463
难道有人能向我表明如何做到这一点?
您可使用rel=“noretinger”>rand()
,与pow(>
<>>>>>a>一起实现:
$digits = 3;
echo rand(pow(10, $digits-1), pow(10, $digits)-1);
这一数字在100至999之间。 这是因为10^2 = 100和10^3=1000,因此,你需要将其与他人分离,以达到预期的范围。
如果 005也是个有效的例子,你就使用以下代码将其排列为零:
$digits = 3;
echo str_pad(rand(0, pow(10, $digits)-1), $digits, 0 , STR_PAD_LEFT);
e.g.
rand ( 10000 , 99999 );
页: 1
Here is a simple solution without any loops or any hassle which will allow you to create random string with characters, numbers or even with special symbols.
$randomNum = substr(str_shuffle("0123456789"), 0, $x);
where $x
can be number of digits
Eg.
substr(str_shuffle("0123456789"), 0, 5);
一起处决后的结果
98450
79324
23017
04317
26479
你们也可以使用同样的法典来进行随机扼杀。
$randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ"), 0, $x);
<代码>x = 11
FgHmqpTR3Ox
O9BsNgcPJDb
1v8Aw5b6H7f
haH40dmAxZf
0EpvHL5lTKr
1. 逐位标出数字,每当一位随机数字:
function n_digit_random($digits) {
$temp = "";
for ($i = 0; $i < $digits; $i++) {
$temp .= rand(0, 9);
}
return (int)$temp;
}
或纯粹的数字解决办法:
function n_digit_random($digits)
return rand(pow(10, $digits - 1) - 1, pow(10, $digits) - 1);
}
一种最简单的思路是使用<代码>rand的功能str_pad
。
<?php
echo str_pad(rand(0,999), 5, "0", STR_PAD_LEFT);
?>
在上述例子中,它将在0至999范围内生成随机编号。
并且有<>5位数。
function random_numbers($digits) {
$min = pow(10, $digits - 1);
$max = pow(10, $digits) - 1;
return mt_rand($min, $max);
}
测试here。
rand(1000, 9999);
works more faster than x4 times rand(0,9);
基准:
rand(1000, 9999) : 0.147 sec.
rand(0,9)x4 times : 0.547 sec.
这两项职能都在100 000英亩,使成果更加明确
你们也可以使用简单易变的实验室功能mt_rand(2000年,9000),从而产生4位数的随机数字。
mt_rand(2000,9000)
您可生成任何以<代码>mt_rand()功能为单位的随机数字。
<代码>mt_rand(>>比rand(>
>快。
Simtax :mt_rand(
or mt_rand($min ,$max)
。
例:<?php remt_rand();?>
a)
function randomWithLength($length){
$number = ;
for ($i = 0; $i < $length; $i++){
$number .= rand(0,9);
}
return (int)$number;
}
rand
or mt_rand
will do...
使用:
rand(min, max);
mt_rand(min, max);
function random_number($size = 5)
{
$random_number= ;
$count=0;
while ($count < $size )
{
$random_digit = mt_rand(0, 9);
$random_number .= $random_digit;
$count++;
}
return $random_number;
}
下面是制定具体长度核查守则的简单方法。 可以通过违约确定长度,产生4位数字代码。
function get_sms_token($length = 4) {
return rand(
((int) str_pad(1, $length, 0, STR_PAD_RIGHT)),
((int) str_pad(9, $length, 9, STR_PAD_RIGHT))
);
}
echo get_sms_token(6);
this simple script will do
$x = 4;//want number of digits for the random number
$sum = 0;
for($i=0;$i<$x;$i++)
{
$sum = $sum + rand(0,9)*pow(10,$i);
}
echo $sum;
这是生成随机数字的另一个简单解决办法:
$number_of_digits = 10;
echo substr(number_format(time() * mt_rand(),0, , ),0,$number_of_digits);
http://codepad.org/pyVvNiof“rel=“nofollow”http://codepad.org/pyVvNiof。
function rand_number_available($already_mem_array,$boundary_min,$boundary_max,$digits_num)
{
$already_mem_array_dim = count($already_mem_array); // dimension of array, that contain occupied elements
// --- creating Boundaries and possible Errors
if( empty($digits_num) ){
$boundary_dim = $boundary_max - $boundary_min;
if($boundary_dim <= 0){
$error = -1; // Error that might happen. Difference between $boundary_max and $boundary_min must be positive
}else{
$error = -2; // Error that might happen. All numbers between, $boundary_min and $boundary_max , are occupied, by $already_mem_array
}
}else{
if($digits_num < 0){ // Error. If exist, $digits_num must be, 1,2,3 or higher
$error = -3;
}elseif($digits_num == 1){ // if one-figure number
$error = -4; // Error that might happen. All one-figure numbers are occupied, by $already_mem_array
$boundary_min = 0;
$boundary_max = 9;
$boundary_dim = $boundary_max-$boundary_min;
}elseif($digits_num == 2){ // if two-figure number
$error = -5; // Error that might happen. All two-figure numbers are occupied, by $already_mem_array
$boundary_min = 10;
$boundary_max = 99;
$boundary_dim = $boundary_max-$boundary_min;
}elseif($digits_num>2){ // if X-figure number. X>2
$error = -6; // Error that might happen. All X-figure numbers are occupied, by $already_mem_array. Unlikely to happen
$boundary_min = pow(10, $digits_num-1); // stepenovanje - graduation
$boundary_max = pow(10, $digits_num)-1;
$boundary_dim = $boundary_max-$boundary_min;
}
}
// -------------------------------------------------------------------
// --- creating response ---------------------------------------------
if( ($already_mem_array_dim <= $boundary_dim) && $boundary_dim>0 ){ // go here only if, there are AVAILABLE numbers to extract, and [difference] $boundary_dim , is positive
do{
$num = rand($boundary_min,$boundary_max);
}while( in_array($num, $already_mem_array) );
$result = $num;
}else{
$result = $error; // Limit that happened
}
return $result;
// -------------------------------------------------------------------
}
请不要说<代码>rand()没有根据 docs产生加密安全价值:
这一功能不会产生加密上的安全价值,不应用于加密目的。 如果你需要加密安全的价值,则考虑使用随机(int)、随机(bytes)或开诚_random_pseudo_bytes(a)。
Instead it is better to use random_int()
, available on PHP 7 (See: http://php.net/manual/en/function.random-int.php).
为了扩大“马克斯”的答复,你应使用:
function generateSecureRandomNumber($digits): int {
return random_int(pow(10, $digits - 1), pow(10, $digits) - 1);
}
function generateSecureRandomNumberWithPadding($digits): string {
$randomNumber = random_int(0, pow(10, $digits) - 1);
return str_pad($randomNumber, $digits, 0 , STR_PAD_LEFT);
}
请注意,如果您不需要安全随机编号,则使用<代码>rand(>)。
下面的编码生成4位数的随机数字:
echo sprintf( "%04d", rand(0,9999));
你们的人真心想使事情复杂化:
真正的问题是,在有些实际数量大的末尾,被占领土可能还要增加。 如果没有,我就没有必要这样做。 由于任何数目的零点都是空洞的。
因此,仅将这一数字的较大部分作为数学数额,而不加以说明。
e.g.
$x = “102384129” .rig_3_数_random_string();
简单明了
$x = 102384129000 + rand(0, 999);
已完成。
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...