burnside Posted October 1, 2008 Share Posted October 1, 2008 Hey, I have a little bit of code : $num = rand(1,10); echo $num; echo $num; echo $num; And it gived me the same number for all 3 echo's witch is what is expected but rather then having three $numa $numb etc ... is it possible to have $num not display the same number twice? if that makes sence? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 1, 2008 Share Posted October 1, 2008 You don't actually change $num in between the echo calls, why in the world would you think it would be different? =/ How about: <?php for ($i=0;$i<3;$i++) { $num[] = rand(1,10); } echo $num[0]; echo $num[1]; echo $num[2]; Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 Did you read your code after your wrote it? You only call the rand() function one time and then echo that number three times. How would it possibly produce more than one random number? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 1, 2008 Share Posted October 1, 2008 Did you read your code after your wrote it? You only call the rand() function one time and then echo that number three times. How would it possibly produce more than one random number? Exactly. =P Quote Link to comment Share on other sites More sharing options...
Zhadus Posted October 1, 2008 Share Posted October 1, 2008 Additionally you could throw it into a function. <?php function newNum() { $num = rand(1,10); return $num; } echo newNum(); echo newNum(); echo newNum(); ?> And as the others have said. If you only randomize once, you only get one random number. If you roll a dice and get a 6, if you look at it two more times without rolling it, it's still a 6 I hope lol. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 Excellent analogy ^^^^ Quote Link to comment Share on other sites More sharing options...
burnside Posted October 1, 2008 Author Share Posted October 1, 2008 no i didnt actually read it after i posted it i answerd my own question any way haha i realised what i was doing wrong after i posted it lol ah well another stupid post for you all to read haha Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 1, 2008 Share Posted October 1, 2008 Additionally you could throw it into a function. <?php function newNum() { $num = rand(1,10); return $num; } echo newNum(); echo newNum(); echo newNum(); ?> And as the others have said. If you only randomize once, you only get one random number. If you roll a dice and get a 6, if you look at it two more times without rolling it, it's still a 6 I hope lol. Wow, that was a reallllly good analogy. *has to remember that one* 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.