Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP and random number generation 1

Status
Not open for further replies.

BigDoug

IS-IT--Management
Feb 20, 2002
49
US
I need help from a PHP expert. I have a HTML form on a webpage and basically what I am trying to do is this. Someone can come to the page and pick a number on a select drop down between 1 and 50, and then submit and I need PHP on the page to generate random 3 digit numbers in the quantity you chose on the select drop down. I.E. You come to the page, and you select 34 for your number. You click submit and the PHP code generates that many random 3 digit numbers in a text area or blank page so you can copy and paste them. How hard is this? Where would a PHP neophyte go to get some help on this? Any help is appreciated.
 
The below code is what I have, but herein lies my problem (bear with me). I need the user to be able to enter a number from 1-50 in a form field and then I want the PHP code to take that number from the form field and generate that many random 3 digit numbers and dosplay them to screen. Currently in this code there is a set number of random 3 digit numbers made (in this case 50) but I want the user to decide what they want displayed. Here is the rudimentary code I have so far:

Code:
<?php

   $numbers = range (100,999);

   srand ((float)microtime()*1000000);
   shuffle ($numbers);

   for ($i=1; $i<=50; $i++) {
     print &quot;$numbers[$i] <BR>&quot;;
   }

?>
 
soooo the user has to enter the number somewhere... let's assume for now it's in the query string... so the url of your page has become

mypage.php?howmany=30

then your script becomes
add this line to your script somewhere near the top
Code:
$howmany = $_GET[&quot;howmany&quot;];
and change your for loop to

Code:
for ($i=1;$i<$howmany;$i++) {

-Rob
 
That's very interesting, and it works! Going out on a help limb here on this question. What I really need to accomplish is that the user can enter the number of random 3 digit numbers they need in a form field. I can use the code you suggest, but I would need to have them enter the number of random 3 digit numbers they want into a url somehow. Isn't there an easy way to grab that number they provide from a form element and generate based on that number? I know, this is basically asking for someone to figure this out for me, and you are right. I don't have time to figure this out (not that I could in any reasonable amount of time).
 
Well sure, you need to make a form with the method=&quot;GET&quot;

then have the action be that page, or if you want to compact things a little, you can do something like this.
Code:
<?php

$howmany = @$_GET[&quot;howmany&quot;];

if (!isset($howmany)) {
  echo &quot;<html><body>
  <form method='get' action=&quot;.$_SERVER[&quot;PHP_SELF&quot;].&quot;>&quot;;
  echo &quot;How many numbers?
  <input type=text name=howmany>
  <input type=submit name=submit value=submit>
  </form>
  </body></html>&quot;;
} else {
   $numbers = range (100,999);

   srand ((float)microtime()*1000000);
   shuffle ($numbers);

   for ($i=1; $i<=$howmany; $i++) {
     print &quot;$numbers[$i] <BR>&quot;;
   }
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top