Random Number Generator
Hi. Do any of you guys know where I can get a free PERL or CGI script that generates a random number between 1 and 1000? (I already check Matts Script Archive). I am a pretty bad programmer!!!! If you guys can write one for me that I can customize, that would be GREAT. Thanks!
Wil posted this at 08:44 — 15th April 2002.
They have: 601 posts
Joined: Nov 2001
#!/usr/local/bin/perl
use strict;
use CGI;
my $query = CGI->new();
print $query->header();
my $number = rand(1000);
print $number;
Mark Hensler posted this at 18:04 — 15th April 2002.
He has: 4,048 posts
Joined: Aug 2000
Question: Is the perl rand() function zero inclusive?
Wil posted this at 19:48 — 15th April 2002.
They have: 601 posts
Joined: Nov 2001
Yes. If no starting range is given it will start from 0 to the given end point.
http://www.perldoc.com/perl5.6.1/pod/func/rand.html
Good question, though - you got me thinking there!
- wil
Mark Hensler posted this at 22:22 — 15th April 2002.
He has: 4,048 posts
Joined: Aug 2000
"fractional number" ... Is the return data type a float? If so, how do you make it a whole number? I didn't see a floor() or round() function (looked here).
Mark Hensler
If there is no answer on Google, then there is no question.
kb posted this at 23:44 — 15th April 2002.
He has: 1,380 posts
Joined: Feb 2002
yea...what about a whole number...does it auto pic a whole number?
also...what if this is to be used in a program...and i dont want to print it...Example:
1- Picks a Random number
2- That number then is filed by a program, which then chooses an image based upon that number
Wil posted this at 09:31 — 16th April 2002.
They have: 601 posts
Joined: Nov 2001
Eskater05,
If you wanted this inside a program then I'd put it into a sub, where you can pass an argument to it - the highest number randomly returned sounds a good one in your case.
sub generate_random_number {
my $high = $_;
my $rand_no = rand($high);
return $rand_no;
}
You would then call this within your program using:
&generate_random_number(1000);
In the above usage example, you want the highest possible random number to be 1000.
Calling your sub will return a random number into $rand_no for you to use within your script (to call your random image, for example).
- wil
Wil posted this at 10:14 — 16th April 2002.
They have: 601 posts
Joined: Nov 2001
Mark
Yes.
For rounding, you have several options:
int, which returns the integer portion of an expression. This is probably not what you're looking for, though.
sprintf, which lets you format your number exactly the way you like it. Remember, there's no real difference betwen string and number variables in perl.
POSIX::ceil or POSIX::floor, which are the equivalent of ceil and floor in C.
See also: [URL=http://www.perldoc.com/perl5.6.1/pod/perlfaq4.html#Does-Perl-have-a-round()-function---What-about-ceil()-and-floor()---Trig-functions-]Does perl have a round function? What about ceil() and floor()? Trig functions?[/URL]
Hope this helps
- wil
kb posted this at 18:49 — 16th April 2002.
He has: 1,380 posts
Joined: Feb 2002
ok...two more questions...
#1- do i put the #!/usr/local/bin/perl stuff if front?
#2- do i put the number 1000 in the _ ?
thanks
kb posted this at 18:52 — 16th April 2002.
He has: 1,380 posts
Joined: Feb 2002
ahh...i'm so bad at this...one more
does the code go within the program or as a separate file (such as rand_number.pl)
thanks again, pardon my thickness
Mark Hensler posted this at 20:20 — 16th April 2002.
He has: 4,048 posts
Joined: Aug 2000
The function can go in the same file. But I *think* that the function must be defined before it is called (so just put it as close to the top of the script as you can).
I forgot about sprintf(). That POSIX module is exactly what I was looking for. Thanks Wil!
Mark Hensler
If there is no answer on Google, then there is no question.
Wil posted this at 08:39 — 17th April 2002.
They have: 601 posts
Joined: Nov 2001
You can just place the sub
sub generate_random_number {
my $high = $_;
my $rand_no = rand($high);
return $rand_no;
}
within your program. You would call it by calling &generate_random_number(1000); anywhere in your program which will return a random number into $rand_no.
Is this what you're looking for?
- wil
kb posted this at 01:38 — 18th April 2002.
He has: 1,380 posts
Joined: Feb 2002
i'm getting confused...can i create this as a .pl file...and then call it up WITHIN the program?
i think i have everything else straight
Wil posted this at 08:22 — 18th April 2002.
They have: 601 posts
Joined: Nov 2001
The easiest thing for you to do would be to place the above sub somewhere WITHIN your current program. That way you can just call it as you would call any other sub using &generate_random_number(1000); but if you put this in a seperate program then you will need to 'require' or 'use' it in at the beginning of your program which basically means more work on your part.
If you're still having difficultes, send me the script to [email protected] and I can make the neccessary ammendmendts.
Cheers
- wil
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.