daGrevis Posted June 23, 2010 Report Share Posted June 23, 2010 <? $random = mt_rand(0, 9999); while(strLen($random) !== 4) { $random = mt_rand(0, 9999); } echo $random; ?> Quote Link to comment Share on other sites More sharing options...
rATRIJS Posted June 23, 2010 Report Share Posted June 23, 2010 echo mt_rand(1000, 9999); Quote Link to comment Share on other sites More sharing options...
daGrevis Posted June 23, 2010 Author Report Share Posted June 23, 2010 Tā, es tikko gribēju *facepalm* un pateikt: "Viss ģeniālais ir vienkāršs!" ...bet atcerējos kāpēc šis variants neder. Pirmkārt iepazīstinu ar savu superīgo pārbaudes funkciju! =D (pārbaudām, vai vienmēr tiek izvadīti četri cipari) <? for($i = 0; $i < 1000000; $i++) { $random = mt_rand(1000, 9999); if(strLen($random) !== 4) { echo 'Problems, Houston! =/'; } } ?> Un tālāk, kāpēc neder. Nevar tikt izvadīts, piemēram, 0101, jo tas nav intervālā starp 1000 un 9999! =/ Nu ir vēl neveselīgais variants... echo mt_rand(0, 9) . mt_rand(0, 9) . mt_rand(0, 9) . mt_rand(0, 9); Bet tas patiešām ir hardcore! =D =D Quote Link to comment Share on other sites More sharing options...
aldis Posted June 23, 2010 Report Share Posted June 23, 2010 Vienu lietu 100 veidos mocīt. :) Diži neko šādā vietā optimizēt nevari. Rand paliek rand, un tie if-i ja nepieciešami, tad tā arī tam jābūt. :) Quote Link to comment Share on other sites More sharing options...
Maaren Posted June 23, 2010 Report Share Posted June 23, 2010 (edited) for ($i=0; $i<=10; $i++) { $nr=rand(1,30)%2; $str .= $nr ? chr(rand(65,90)) : chr(rand(48,57)); } echo $str; Neatceros vai pats šo izgudroju. Bet scripts ģenerē burtus,cīparus. Edited June 23, 2010 by Maaren Quote Link to comment Share on other sites More sharing options...
rATRIJS Posted June 23, 2010 Report Share Posted June 23, 2010 Nu tava pirmais koda gabals ar netaisiitu taadus stringus (0101). Reku tev kods uzvarai: echo str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT); Quote Link to comment Share on other sites More sharing options...
Kavacky Posted June 28, 2010 Report Share Posted June 28, 2010 Paddojamo simbolu arī vajag random, pie tam likt klāt pa vienam, katru reizi randomā. Tā būs PILNĪGA uzvara. Quote Link to comment Share on other sites More sharing options...
codez Posted June 28, 2010 Report Share Posted June 28, 2010 (edited) ja tikai ciparus function ng($l){ for($s='';$l;$l--,$s.=rand(0,9)); return $s; } ja arī burtus, tad te- rs5. Edited June 28, 2010 by codez Quote Link to comment Share on other sites More sharing options...
v3rb0 Posted June 28, 2010 Report Share Posted June 28, 2010 tur vēl divus simbolus var izmest (: .. for($s='';$l--;$s.=rand(0,9)); .. Quote Link to comment Share on other sites More sharing options...
codez Posted June 28, 2010 Report Share Posted June 28, 2010 tur vēl divus simbolus var izmest (: .. for($s='';$l--;$s.=rand(0,9)); .. ir labs, šito neiedomājos. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.