#### Archived

This topic is now archived and is closed to further replies.

# random number

## Recommended Posts

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,

##### Share on other sites
Whats your random number code? TIA

##### Share on other sites
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.

##### Share on other sites
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);
?>

##### Share on other sites
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]

##### Share on other sites
[color=red]rand[/color][color=blue]([/color][b]minimum[/b][color=blue],[/color][b]maximum[/b][color=blue])[/color]

##### Share on other sites
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