slyte33 Posted December 3, 2009 Share Posted December 3, 2009 Basically I'm making crimes in my game, you enter how many turns you want to use then foreach turn do something: $times = $_POST[turns]; for($i=1; $i<=$times; $i++) { echo "#$i: "; $query=$db->execute("select * from allpitems"); while($item = $query->fetchrow()) { $rand = mt_rand(1, $item[itemfind]); if ($rand == $item[itemfind]) { echo "You found $item[name]<br><br>"; }else if ($rand != $item[itemfind]){ echo "You did not find anything<br><br>"; } } Since this is a while loop inside a for loop, each $i is going to loop X times(X being the number of rows in the table all_items). I know a way to stop it and fix this, by going about a different way: What I want to do is make it generate a set of numbers, to be exact these numbers: 1, 2, 4, 8, 33, 83, 122, 125 so instead of using: mt_rand(1, 125); I only want it to generate the numbers I want, is this possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183870-generating-a-random-number-by-your-set-values/ Share on other sites More sharing options...
Daniel0 Posted December 3, 2009 Share Posted December 3, 2009 http://en.wikipedia.org/wiki/Indent_style Pick one and use it. Unindented/unformatted code is difficult to read for humans. What I want to do is make it generate a set of numbers, to be exact these numbers: 1, 2, 4, 8, 33, 83, 122, 125 How is it random when you've already decided that you want exactly those numbers? Maybe I'm missing something... Quote Link to comment https://forums.phpfreaks.com/topic/183870-generating-a-random-number-by-your-set-values/#findComment-970665 Share on other sites More sharing options...
Maq Posted December 3, 2009 Share Posted December 3, 2009 I think slyte33 means something like this: $arr=array(1, 2, 4, 8, 33, 83, 122, 125); echo $arr[rand(0,count($arr))]; ?> Same concept as a bag in Java. Quote Link to comment https://forums.phpfreaks.com/topic/183870-generating-a-random-number-by-your-set-values/#findComment-970668 Share on other sites More sharing options...
Daniel0 Posted December 3, 2009 Share Posted December 3, 2009 Ah. In that case there is actually a function called array_rand: echo $arr[array_rand($arr)]; Quote Link to comment https://forums.phpfreaks.com/topic/183870-generating-a-random-number-by-your-set-values/#findComment-970674 Share on other sites More sharing options...
slyte33 Posted December 3, 2009 Author Share Posted December 3, 2009 http://en.wikipedia.org/wiki/Indent_style Pick one and use it. Unindented/unformatted code is difficult to read for humans. What I want to do is make it generate a set of numbers, to be exact these numbers: 1, 2, 4, 8, 33, 83, 122, 125 How is it random when you've already decided that you want exactly those numbers? Maybe I'm missing something... I want those numbers exactly because i'm making crimes, some crimes are steal food, steal clothes, steal jewelry. Each item you can own is in the all_items table with a set type(enum) and a set item_id When stealing clothes, I want those item_id's only to be picked in a random sequence, so therefore you'd only able to steal clothes and not every type of item in the database This is I think slyte33 means something like this: <?php $arr=array(1, 2, 4, 8, 33, 83, 122, 125); echo $arr[rand(0,count($arr))]; ?> Same concept as a bag in Java. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/183870-generating-a-random-number-by-your-set-values/#findComment-970675 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.