I-AM-OBODO Posted April 21, 2012 Share Posted April 21, 2012 Hi all, sorry cos there is no code. please how can i generate pairs (in twos, threes, fours or five) from a given set of numbers. the numbers are to be typed in the form text field and when click generate(submit button) it brings out the pair of numbers. thanks in advance my form <form id="form1" name="form1" method="post" action=""> <table width="100%" border="0" cellspacing="2" cellpadding="1"> <tr> <td width="21%">Input Numbers : </td> <td width="79%"><input name="numbers" type="text" id="numbers" size="50" /></td> </tr> <tr> <td> </td> <td><input name="generate" type="submit" id="generate" value="Generate" /></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan="2"> </td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/261382-random-number-generation/ Share on other sites More sharing options...
MMDE Posted April 21, 2012 Share Posted April 21, 2012 If you already have the numbers you want to pick a random from, then you could put it in an array and use shuffle and from there use the first in the array, but what is far more recommended is to instead generate a random number with mt_rand and by that number pick one at random from the array. Example: <?php $numbers[] = 10; $numbers[] = 5; $numbers[] = 0; $numbers[] = 7; $numbers[] = 13; $numbers[] = 9; $numbers[] = 25; $numbers[] = 4; echo $numbers[mt_rand(0,count($numbers)-1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261382-random-number-generation/#findComment-1339389 Share on other sites More sharing options...
I-AM-OBODO Posted April 22, 2012 Author Share Posted April 22, 2012 If you already have the numbers you want to pick a random from, then you could put it in an array and use shuffle and from there use the first in the array, but what is far more recommended is to instead generate a random number with mt_rand and by that number pick one at random from the array. Example: <?php $numbers[] = 10; $numbers[] = 5; $numbers[] = 0; $numbers[] = 7; $numbers[] = 13; $numbers[] = 9; $numbers[] = 25; $numbers[] = 4; echo $numbers[mt_rand(0,count($numbers)-1]; ?> Thank you. but why i am using my own number instead of generating numbers with mt_rand() is because, i want the user to be able to input their own series of numbers so that the program shuffles and bring them in pairs. Quote Link to comment https://forums.phpfreaks.com/topic/261382-random-number-generation/#findComment-1339459 Share on other sites More sharing options...
I-AM-OBODO Posted April 22, 2012 Author Share Posted April 22, 2012 This is what i have done so far, but two problems; 1, the last number is duplicated 2, how can i make the numbers come in pair of three (12, 16, 18) thank you form <form id="form1" name="form1" method="post" action="show.php"> <table width="100%" border="0" cellspacing="2" cellpadding="1"> <tr> <td width="21%">Input Numbers : </td> <td width="79%"><input name="one" type="text" id="one" size="10" /></td> </tr> <tr> <td width="21%"> </td> <td><input name="two" type="text" id="two" size="10" /></td> </tr> <tr> <td width="21%"> </td> <td><input name="three" type="text" id="three" size="10" /></td> </tr> <tr> <td width="21%"> </td> <td><input name="four" type="text" id="four" size="10" /></td> </tr> <tr> <td width="21%"> </td> <td><input name="five" type="text" id="five" size="10" /></td> </tr> <tr> <td width="21%"> </td> <td><input name="six" type="text" id="six" size="10" /></td> </tr> <tr> <td> </td> <td><input name="seven" type="text" id="seven" size="10" /></td> </tr> <tr> <td> </td> <td><input name="eight" type="text" id="eight" size="10" /></td> </tr> <tr> <td> </td> <td><input name="nine" type="text" id="nine" size="10" /></td> </tr> <tr> <td> </td> <td><input name="ten" type="text" id="ten" size="10" /></td> </tr> <tr> <td> </td> <td><input name="generate" type="submit" id="generate" value="Generate" /></td> </tr> <tr> <td colspan="2"> </td> </tr> </table> </form> show.php <?php $one = $_POST['one']; $two = $_POST['two']; $three = $_POST['three']; $four = $_POST['four']; $five = $_POST['five']; $six = $_POST['six']; $seven = $_POST['seven']; $eight = $_POST['eight']; $nine = $_POST['nine']; $ten = $_POST['ten']; $bingo = array($one,$two,$three,$four,$five,$six,$seven,$eight,$nine,$ten); shuffle($bingo); foreach ($bingo as $number) { echo "$number "; } echo $number; ?> Quote Link to comment https://forums.phpfreaks.com/topic/261382-random-number-generation/#findComment-1339462 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.