# random number

6 replies to this topic

### #1 bob_rock

bob_rock
• New Members
• Newbie
• 7 posts

Posted 24 October 2006 - 10:35 AM

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,

### #2 matfish

matfish
• Members
• 242 posts
• LocationUK

Posted 24 October 2006 - 10:38 AM

Whats your random number code? TIA

### #3 heckenschutze

heckenschutze
• Members
• 257 posts
• LocationAustralia

Posted 24 October 2006 - 10:47 AM

Something like;

```<?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.
?>```

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.

### #4 bob_rock

bob_rock
• New Members
• Newbie
• 7 posts

Posted 24 October 2006 - 10:51 AM

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 ...
<?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);
?>

### #5 alpine

alpine
• Members
• 756 posts
• LocationNorway

Posted 24 October 2006 - 12:08 PM

If i understand your last post correctly you want to randomize the string "Nina" ?
```<?php

\$beseda = 'Nina';
\$pieces = str_split(\$beseda);
shuffle(\$pieces);
foreach(\$pieces as \$piece)
{
echo \$piece;
}

?>
```

### #6 True`Logic

True`Logic
• Members
• 59 posts

Posted 24 October 2006 - 03:38 PM

rand(minimum,maximum)

### #7 SharkBait

SharkBait
• Members
• 845 posts
• LocationMetro Vancouver, BC

Posted 24 October 2006 - 03:43 PM

If i understand your last post correctly you want to randomize the string "Nina" ?

```<?php

\$beseda = 'Nina';
\$pieces = str_split(\$beseda);
shuffle(\$pieces);
foreach(\$pieces as \$piece)
{
echo \$piece;
}

?>
```

There is also
`str_shuffle('Nina');`
Though you need a version of at least 4.3 in PHP

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users