Jump to content

Archived

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

bob_rock

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.