beansandsausages 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? Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/ 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]; Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/#findComment-654743 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? Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/#findComment-654744 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 Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/#findComment-654745 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. Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/#findComment-654746 Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 Excellent analogy ^^^^ Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/#findComment-654748 Share on other sites More sharing options...
beansandsausages 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 Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/#findComment-654751 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* Link to comment https://forums.phpfreaks.com/topic/126618-solved-random-number-problem/#findComment-654752 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.