php如何生成十进制00到20之间的2位随机数
2013-12-21 来自:不要捉急啊! 9 人回应

比如随机数是0, 就是00 随机数是15, 就是15 输出的是2位数

2013-12-25 来自:过尘

接口呢?类呢?面向对象呢?

<?php

interface RandomGenerator {
public function next();
}

interface Format {
public function format($num);
}

class RandomBetweenZeroAndTwenty implements RandomGenerator {
public function next() {
return rand(0,20);
}
}

class ZeroPaddingFormat implements Format {
private $length;

function __construct($length) {
$this->length = $length;
}
public function format($num) {
$str = sprintf("%d", $num);
if (strlen($str) > $this->length) {
return $str;
} else {
return str_repeat("0", $this->length - strlen($str)) . $str;
}

}
}

class RandomNum {
private $generator;
private $formator;

function __construct(RandomGenerator $g, Format $r) {
$this->generator = $g;
$this->formator = $r;
}

function gen() {
return $this->formator->format($this->generator->next());
}
}

class Test {

public function run() {
$rng = new RandomNum(new RandomBetweenZeroAndTwenty(), new ZeroPaddingFormat(2));
echo $rng->gen();
}
}

(new Test())->run();
2013-12-25 来自:纯洁的、小孩
function random_text($count, $rm_similar = true)
{
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));

// remove similar looking characters
if ($rm_similar)
{
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
}
$text = '';
for ($i = 0; $i < $count; $i++)
{
$text .= array_rand($chars);
}
return $text;

}
2013-12-24 来自:精明的驴子
function randnum(){
$number = rand(0,20);
if($number<10){
$number='0'.$number;
}
return $number;
}
2013-12-24 来自:小扣子

没有注意审题,只是提供一个思路

function randnum(){
//$number = rand(0,2).rand(0,9);
$tens = rand(0,2);
$one = $tens === 2 ? 0 : rand(0,9);
return $tens.$one;
}
echo randnum();
2013-12-24 来自:Bean
function my_random(){
return sprintf("%'02d", rand(0,20)); #left padding with 0
}
2013-12-23 来自:辜馨
function my_random(){
return sprintf("%'02d", rand(0,20)); #left padding with 0
}
2013-12-23 来自:好运猪
function random_text($count, $rm_similar = true)
{
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));

// remove similar looking characters
if ($rm_similar)
{
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
}
$text = '';
for ($i = 0; $i < $count; $i++)
{
$text .= array_rand($chars);
}
return $text;

}
2013-12-23 来自:袷衣
function randnum(){
$number = rand(0,20);
if($number<10){
$number='0'.$number;
}
return $number;
}
2013-12-22 来自:strawberry
function random_text($count, $rm_similar = true)
{
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));

// remove similar looking characters
if ($rm_similar)
{
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
}
$text = '';
for ($i = 0; $i < $count; $i++)
{
$text .= array_rand($chars);
}
return $text;

}

您的回应

你还未登陆,不能回应!登陆