Jump to content

Random Number Generation


I-AM-OBODO

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/261382-random-number-generation/
Share on other sites

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];
?>

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.

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;
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.