Driiper Posted April 15, 2008 Share Posted April 15, 2008 Hello! i creating a game "School project" and then i was wondering how can i have 1 string with rand() instead of 20.. if i have just 1 rand the random number is the same :S Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/101186-how-to-have-20-rand-in-1-string/ Share on other sites More sharing options...
craygo Posted April 15, 2008 Share Posted April 15, 2008 not sure what you want to do, but you can set rand to give a random number between 2 numbers. $random = rand(0, 100); now you will get a random number between 0 and 100. If that is not what you were looking for then please explain a little better. Or give an example of what you want to accomplish. Ray Link to comment https://forums.phpfreaks.com/topic/101186-how-to-have-20-rand-in-1-string/#findComment-517597 Share on other sites More sharing options...
ucffool Posted April 15, 2008 Share Posted April 15, 2008 I think what you want is one string that contains the output of rand() run 20 times? $string = ''; for ($x=0; $x<20; $x++){ $string .= rand(); // concatenate onto string the result of rand() } echo $string; Link to comment https://forums.phpfreaks.com/topic/101186-how-to-have-20-rand-in-1-string/#findComment-517980 Share on other sites More sharing options...
Orio Posted April 15, 2008 Share Posted April 15, 2008 @ucffool - Won't work like that... From the manual: If called without the optional min , max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX This should work: <?php $string = ''; for ($x=0; $x<20; $x++) $string .= rand(0,9); // concatenate onto string the result of rand() echo $string; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/101186-how-to-have-20-rand-in-1-string/#findComment-517988 Share on other sites More sharing options...
ucffool Posted April 15, 2008 Share Posted April 15, 2008 Assuming he is looking for 1 number only. I realize that's the way rand() will work, I was just getting the bigger concept across. Either way, I suppose it is good someone clarified. I would recommend mt_rand() or mt_rand(0,9) instead. Link to comment https://forums.phpfreaks.com/topic/101186-how-to-have-20-rand-in-1-string/#findComment-518011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.