str_random
[Str::random[]
] cố gắng sử dụng openssl_random_pseudo_bytes
, đó là một trình tạo số ngẫu nhiên giả được tối ưu hóa cho mật mã, không phải là tính duy nhất. Nếu openssl_random_pseudo_bytes
không có sẵn, nó sẽ trở lại quickRandom[]
:
public static function quickRandom[$length = 16]
{
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
return substr[str_shuffle[str_repeat[$pool, 5]], 0, $length];
}
Theo tôi, mã quickRandom
không đáng tin cậy đối với tính độc đáo cũng như mật mã.is not reliable for uniqueness nor cryptography.
Có, có openssl_random_pseudo_bytes
và sử dụng 32 byte gần như không thể nhìn thấy một vụ va chạm, nhưng vẫn có thể. Nếu bạn muốn đảm bảo chuỗi/số của bạn sẽ là duy nhất [99,99%], bạn nên sử dụng hàm UUID tốt hơn. Đây là những gì tôi thường sử dụng:
/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
Nó tạo ra UUID phiên bản 4211 hợp lệ của RFC 4211.
Kiểm tra điều này: //en.wikipedia.org/wiki/universally_unique_identifier#collutions
szeryf.wordpress.com
11 năm trước — Generate a random integer
Sự mô tả
Rand [] đã nhận được một sửa lỗi cho lỗi thiên vị modulo. Điều này có nghĩa là các trình tự được tạo ra với một hạt giống cụ thể có thể khác với PHP 7.1 trên các máy 64 bit.[]: int
rand [] đã được tạo thành một bí danh của mt_rand [].[int $min
, int
0]: int/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
Ví dụrand[] returns a pseudo-random integer between 0 and getrandmax[]. If you want a random number between 5 and 15 [inclusive], for example, use
/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
3. Ví dụ #1 rand [] ví dụ
Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:
Ghi chúRandom\Randomizer may be used with the Random\Engine\Secure engine. For simple use cases, the random_int[] and random_bytes[] functions provide a convenient and secure API that is backed by the operating system’s CSPRNG.
Lưu ý: Trên một số nền tảng [chẳng hạn như Windows], getRandMax [] chỉ là 32767. Nếu bạn yêu cầu phạm vi lớn hơn 32767, chỉ định
1 và/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
2 sẽ cho phép bạn tạo một phạm vi lớn hơn mức này hoặc xem xét sử dụng MT_RAND [].: On some platforms [such as Windows], getrandmax[] is only 32767. If you require a range larger than 32767, specifying/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
1 and/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
2 will allow you to create a range larger than this, or consider using mt_rand[] instead./** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
Lưu ý: Kể từ Php 7.1.0, rand [] sử dụng cùng một trình tạo số ngẫu nhiên như mt_rand []. Để bảo tồn khả năng tương thích ngược Rand [] cho phép
2 nhỏ hơn/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
1 trái ngược với việc trả lại/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
8 dưới dạng mt_rand [].: As of PHP 7.1.0, rand[] uses the same random number generator as mt_rand[]. To preserve backwards compatibility rand[] allows/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
2 to be smaller than/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
1 as opposed to returning/** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
8 as mt_rand[]./** * * Generate v4 UUID * * Version 4 UUIDs are pseudo-random. */ public static function v4[] { 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] ]; }
Thông số
/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
1Giá trị thấp nhất để trả về [mặc định: 0]
/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
2Giá trị cao nhất để trả về [mặc định: getRandMax []]getrandmax[]]
Trả về giá trị
Giá trị ngẫu nhiên giả giữa
/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
1 [hoặc 0] và /**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
2 [hoặc getrandmax [], bao gồm].getrandmax[], inclusive]. Thay đổi
7.2.0 | Rand [] đã nhận được một sửa lỗi cho lỗi thiên vị modulo. Điều này có nghĩa là các trình tự được tạo ra với một hạt giống cụ thể có thể khác với PHP 7.1 trên các máy 64 bit. 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. |
7.1.0 | rand [] đã được tạo thành một bí danh của mt_rand []. has been made an alias of mt_rand[]. |
Ví dụ
Ví dụ #1 rand [] ví dụrand[] example
str_random
3
str_random
4
str_random
5Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:
Ghi chú
Cảnh báo
/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
1 /**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
2 Phạm vi phải nằm trong phạm vi getrandmax []. tức là [/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
2 - /**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
1]getrandmax[]. i.e. [/**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
2 - /**
*
* Generate v4 UUID
*
* Version 4 UUIDs are pseudo-random.
*/
public static function v4[]
{
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]
];
}
1]