bob_rock Posted October 24, 2006 Share Posted October 24, 2006 hello , I'm new to php , and i'm trying to make a script that will write down random numbers for example form 0-9.I know how to do this , but i don't want the numbers to repeat.thank you, Quote Link to comment Share on other sites More sharing options...
matfish Posted October 24, 2006 Share Posted October 24, 2006 Whats your random number code? TIA Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted October 24, 2006 Share Posted October 24, 2006 Something like;[code]<?php$iRandMin = 0;$iRandMax = 9;$iNumOfRandomInts = 5;$aNumbers = array();for($i = 0; $i < $iNumOfRandomInts;){ $iRand = rand($iRandMin, $iRandMax); if(in_array($iRand, $aNumbers)) { continue; }else{ $aNumbers[] = $iRand; $i++; }}print_r($aNumbers); //** array with your random numbers in it.?>[/code]I dunno if there are any php funcs to do this, so yeah...Tip: If your going to use this, turn it into a function :)Note: Mathematically, this could take forever, if the values that come up are already in the array...hth. Quote Link to comment Share on other sites More sharing options...
bob_rock Posted October 24, 2006 Author Share Posted October 24, 2006 Here it is, don't know if this is ok , but it's almost working.It reads the number of letters in the word and then it randomly prints out the letters.The anoying thing is that the letters repeat...Sorry for my bad english ... :P<?php $beseda='Nina';$sum=strlen($beseda);$i=1;$c=$sum-1;do {$a=$sum-($sum-$i);$i++;$b=rand(0,"$c");$crka=$beseda[$b];echo $crka; } while($a<$sum);?> Quote Link to comment Share on other sites More sharing options...
alpine Posted October 24, 2006 Share Posted October 24, 2006 If i understand your last post correctly you want to randomize the string "Nina" ?[code]<?php$beseda = 'Nina';$pieces = str_split($beseda);shuffle($pieces);foreach($pieces as $piece){echo $piece;}?>[/code] Quote Link to comment Share on other sites More sharing options...
True`Logic Posted October 24, 2006 Share Posted October 24, 2006 [color=red]rand[/color][color=blue]([/color][b]minimum[/b][color=blue],[/color][b]maximum[/b][color=blue])[/color] Quote Link to comment Share on other sites More sharing options...
SharkBait Posted October 24, 2006 Share Posted October 24, 2006 [quote author=alpine link=topic=112513.msg456753#msg456753 date=1161691682]If i understand your last post correctly you want to randomize the string "Nina" ?[code]<?php$beseda = 'Nina';$pieces = str_split($beseda);shuffle($pieces);foreach($pieces as $piece){echo $piece;}?>[/code][/quote]There is also [code=php:0]str_shuffle('Nina');[/code] Though you need a version of at least 4.3 in PHP 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.