好!欢迎访问迷津渡口 现在时间是: 天气 °C ~ °C

迷津渡口

越感到迷惑 越接近真理

PHP教程-如何产生不重复随机数和随机字符

随机数和随机字符在PHP项目中很常用,小指总结了几种常见的方法:

rand():返回某个范围内的一个随机数

// 返回1-50内的某个随机数
echo rand(1,50);

优点:速度快,要求不高时实用。

缺点:重复几率较大,在获取多个随机数时,无法控制重复。


range()+shuffle()+array_slice():返回某个范围内的一组不重复的随机数

// 获取1-50的数组
$numarr=range(1,50);
// 打乱数组排列
shuffle($numarr);
// 返回一组5个不重复的随机数
$result=array_slice($numarr,0,5);
print_r($result);


自定义数组+array_rand():返回数组内一组不重复的键值

// 自定义数组
$arr=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
// 获取长度为2的随机键构成的数组
$arrkeys=array_rand($arr,2);
// 获取键值对应的数组值
echo $arr[$arrkeys[0]]."<br>".$arr[$arrkeys[1]];


自定义数组+shuffle()+array_slice():返回自定义数组内一组随机不重复的值

// 自定义数组
$arr=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
// 随机打乱数组,注意此时键均被转化为数字键
shuffle($arr);
// 返回2个不重复的随机值
$result=array_slice($arr,0,2);
print_r($result);


自定义字符串+str_shuffle()+substr():返回自定义字符串内一组随机字符串

// 大小写和数字组成的字符串
$str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// 随机打乱字符串
str_shuffle($str);
// 返回长度为8的随机字符串
echo subtr($str,0,8);

常用于生成验证码,密码等。

另一种用法:可能生成重复字符,但相对随机:

// 大小写和数字组成的字符串
$str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$strresult='';
for ($i=0;$i<8;$i++) {
    // 随机打乱字符串
    str_shuffle($str);
    // 返回长度为8的随机字符串
    $strresult.=subtr($str,0,1);
}
echo $strresult;


以上总结的都是实用常用的代码,其他的有更好的欢迎分享~

点赞233
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
autosasart
 autosasart 约 5 小时前回复
#100
<a href="http://www.masterelief.com/nike-air-max-2014-kvinders-gr%C3%A5-s%C3%B8lv-schuheg">nike air max 2014 kvinders gr氓 s酶lv</a> <a href="http://www.nwlcenter.com/adidas-neo-raleigh-high-top-sneaker-maschio-day-kurpesc">adidas neo raleigh high top sneaker maschio day</a> <a href="http://www.piezoinjector.com/nike-free-5.0-verde-and-nero-obuvg">nike free 5.0 verde and nero</a> <a href="http://www.printonlipro.com/nike-huarache-marinen-pure-platinum-scarped">nike huarache marinen pure platinum</a> <a href="http://www.biagricola.com/air-jordan-1-low-sail-sport-fuschia-mettoutic-or-noir-butya">air jordan 1 low sail sport fuschia mettoutic or noir</a> <a href="http://www.dennygorman.com/nike-shox-deliver-rosa-and-nero-belt-chaussuresf">nike shox deliver rosa and nero belt</a>
autosasart http://www.autosasart.com/
marjalfun
 marjalfun 约 9 小时前回复
#99
<a href="http://www.sguerradesign.com/nike-air-jordan-retro-10-powder-azul-zapatosk">nike air jordan retro 10 powder azul</a> <a href="http://www.zeneventsja.com/kobe-10-elite-negro-blanco-zapatosp">kobe 10 elite negro blanco</a> <a href="http://www.adharaweb.com/north-face-resolve-jacket-northfaceh">north face resolve jacket</a> <a href="http://www.jrelselaw.com/nike-air-max-1-dam%C3%A4nner-lila-blau-schuhej">nike air max 1 dam盲nner lila blau</a> <a href="http://www.intellalegal.com/aidas-boost-clima-chill-r%C3%B8d-gr%C3%A5-skoh">aidas boost clima chill r酶d gr氓</a> <a href="http://www.carmelscoffee.com/nike-air-max-97-rouge-bleu-ciel-chaussuresh">nike air max 97 rouge bleu ciel</a>
marjalfun http://www.marjalfun.com/
plusbolivia
 plusbolivia 2019-03-20 01:52回复
#98
<a href="http://www.champste.com/nike-hyperdunk-2014-rouge-orange-chaussuresh">nike hyperdunk 2014 rouge orange</a> <a href="http://www.rollaname.com/ni%C3%B1os-verde-converse-zapatos-zapatosk">ni帽os verde converse zapatos</a> <a href="http://www.sfcigar.com/nike-tiempo-legend-black-black-shoesj">nike tiempo legend black black</a> <a href="http://www.slotoyunpro.com/pink-san-francisco-giants-hat-hath">pink san francisco giants hat</a> <a href="http://www.eistyles.com/adidas-ace-15.1-tf-plata-verde-zapatosl">adidas ace 15.1 tf plata verde</a> <a href="http://www.filmoriental.com/nike-free-run-3-yellow-shoesk">nike free run 3 yellow</a>
plusbolivia http://www.plusbolivia.com/
cutlaroc
 cutlaroc 2019-03-19 19:46回复
#97
<a href="http://www.setrails.com/air-jordan-retro-11-schwarz-himmelblau-schuhec">air jordan retro 11 schwarz himmelblau</a> <a href="http://www.sidestoreid.com/sabres-11-gilbert-perreault-black-ccm-throwback-stitched-nhl-jersey-nflr">sabres 11 gilbert perreault black ccm throwback stitched nhl jersey</a> <a href="http://www.beledimuebles.com/mens-nike-miami-dolphins-17-ryan-tannehill-limited-aqua-green-strobe-nfl-jersey-nflw">mens nike miami dolphins 17 ryan tannehill limited aqua green strobe nfl jersey</a> <a href="http://www.eulatest.com/nike-free-5.0-nero-grigio-argento-schuheg">nike free 5.0 nero grigio argento</a> <a href="http://www.redshadowclub.com/nike-zoom-kobe-venomennon-iv-4-xdr-scarpef">nike zoom kobe venomennon iv 4 xdr</a> <a href="http://www.willymunoz.com/nike-air-max-tailwind-7-sort-lyser%C3%B8d-kurpese">nike air max tailwind 7 sort lyser酶d</a>
cutlaroc http://www.cutlaroc.com/
swisshomeas
 swisshomeas 2019-03-18 21:09回复
#96
<a href="http://www.seymourdugan.com/nike-air-max-90-vt-gr%C3%B8nn-obuvl">nike air max 90 vt gr酶nn</a> <a href="http://www.dkstekstil.com/nike-air-huarache-s%C3%B8lv-gr%C3%B8n-skoj">nike air huarache s酶lv gr酶n</a> <a href="http://www.thegillssell.com/nike-zoom-pegasus-34-blu-verde-scarpel">nike zoom pegasus 34 blu verde</a>
swisshomeas http://www.swisshomeas.com/
allikatri
 allikatri 2019-03-18 06:50回复
#95
<a href="http://www.onlype.com/air-jordan-7-alle-colorways-schuheb">air jordan 7 alle colorways</a> <a href="http://www.renitemare.com/nfl-womens-nike-pittsburgh-steelers-24-coty-sensabaugh-black-backer-slim-fit-t-shirt-nflr">nfl womens nike pittsburgh steelers 24 coty sensabaugh black backer slim fit t shirt</a> <a href="http://www.alusownia.com/nike-mens-cornellius-carradine-limited-olive-jersey-oakland-raiders-nfl-96-2017-salute-to-service-nflw">nike mens cornellius carradine limited olive jersey oakland raiders nfl 96 2017 salute to service</a> <a href="http://www.godskingdoms.com/air-max-90-r%C3%B8d-bl%C3%A5-hvit-schuheg">air max 90 r酶d bl氓 hvit</a> <a href="http://www.jemegaupload.com/asics-gel-quantum-360-nero-knit-loose-scarpeg">asics gel quantum 360 nero knit loose</a> <a href="http://www.ratedpractice.com/air-jordan-1-retro-powder-blau-zone-kurpese">air jordan 1 retro powder blau zone</a>
allikatri http://www.allikatri.com/
dianegrams
 dianegrams 2019-03-17 19:52回复
#94
<a href="http://www.goodmedusa.com/nike-free-run-2-dam%C3%A4nner-schwarz-schuhej">nike free run 2 dam盲nner schwarz</a> <a href="http://www.murskacorp.com/frau-air-jordan-retro-10-schwarz-lila-schuhei">frau air jordan retro 10 schwarz lila</a> <a href="http://www.prajansim.com/nike-free-run-5.0-kvinner-lime-lilla-obuvj">nike free run 5.0 kvinner lime lilla</a> <a href="http://www.pokersurga.com/asics-gel-kinsei-5-maschio-periwinkle-flash-gituttio-mint-scarpel">asics gel kinsei 5 maschio periwinkle flash gituttio mint</a> <a href="http://www.arkmmj.com/dark-gris-converse-hommes-chaussuresh">dark gris converse hommes</a> <a href="http://www.butterbroad.com/adidas-tubular-defiant-core-nero-scarpeh">adidas tubular defiant core nero</a>
dianegrams http://www.dianegrams.com/
paulabandura
 paulabandura 2019-03-17 08:14回复
#93
<a href="http://www.thecarolinatc.com/bon-march%C3%A9-nike-shox-avenue-femmes-vert-sneakerc">bon march茅 nike shox avenue femmes vert</a> <a href="http://www.ventnorlife.com/hermes-small-shoulder-bag-hermesr">hermes small shoulder bag</a> <a href="http://www.edenwins.com/nike-air-force-1-puerto-rico-sko-shoesv">nike air force 1 puerto rico sko</a> <a href="http://www.viagradc.com/coach-diaper-bag-baby-blue-cross-coachc">coach diaper bag baby blue cross</a> <a href="http://www.duranerdogan.com/air-max-1-huarache-hybrid-shoesc">air max 1 huarache hybrid</a> <a href="http://www.newmadagasca.com/ladies-black-adidas-gazelle-skos">ladies black adidas gazelle</a>
paulabandura http://www.paulabandura.com/
emtecsolution
 emtecsolution 2019-03-16 18:16回复
#92
<a href="http://www.publiswiss.net/nike-air-force-1-mid-r%C3%B8d-lilla-butya">nike air force 1 mid r酶d lilla</a> <a href="http://www.puffyp.net/adidas-rose-mujeres-zapatos-quality-scarpee">adidas rose mujeres zapatos quality</a> <a href="http://www.radiosbestfriend.net/adidas-climacool-ride-v-hvid-gul-butyf">adidas climacool ride v hvid gul</a> <a href="http://www.relaxmassagestudio.net/nike-lebron-12-rosso-and-nero-pants-butyf">nike lebron 12 rosso and nero pants</a> <a href="http://www.risaralda.net/adidas-superstar-enfants-rouge-jaune-schuhea">adidas superstar enfants rouge jaune</a> <a href="http://www.santfeliucentre.net/air-jordan-retro-11-barn-rosa-gul-chaussuresa">air jordan retro 11 barn rosa gul</a>
emtecsolution http://www.emtecsolution.net/
powersellingmom
 powersellingmom 2019-03-16 17:27回复
#91
<a href="http://www.audioacousticart.net/air-max-1-negro-p%C3%BArpura-scarpec">air max 1 negro p煤rpura</a> <a href="http://www.auditionrush.net/hombres-nike-air-huarache-light-negro-p%C3%BArpura-schuhee">hombres nike air huarache light negro p煤rpura</a> <a href="http://www.autismy.net/air-jordan-6-gs-bright-lilla-scarpeb">air jordan 6 gs bright lilla</a> <a href="http://www.automotoracing.net/nike-foamposite-gone-fishing-for-venta-oregon-schuhee">nike foamposite gone fishing for venta oregon</a> <a href="http://www.ave-koeln.net/jordan-bel-air-argent-violet-butyf">jordan bel air argent violet</a> <a href="http://www.awffull.net/asics-gel-noosa-tri-9-violet-hair-obuve">asics gel noosa tri 9 violet hair</a>
powersellingmom http://www.powersellingmom.net/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸