Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > rand() question

Reply
Thread Tools

rand() question

 
 
daniel kaplan
Guest
Posts: n/a
 
      09-15-2004
so i generate a serial number with the exact code below (three lines):

$number1 = 10000 + int(rand(99999));
$number2 = 10000 + int(rand(99999));
$shrwsn = "$number1-$number2";

the point is i want TWO five digit numbers separated by a dash "-"
and all is cool, but i just noticed, that for the three entries i have made
test wise:

79109-72626
106421-1023 these last two are six and four digits.....
108629-3542

WHY? these were all made with the SAME script...............


 
Reply With Quote
 
 
 
 
Marc Dashevsky
Guest
Posts: n/a
 
      09-15-2004
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> so i generate a serial number with the exact code below (three lines):
>
> $number1 = 10000 + int(rand(99999));
> $number2 = 10000 + int(rand(99999));
> $shrwsn = "$number1-$number2";
>
> the point is i want TWO five digit numbers separated by a dash "-"


So why don't you do:

$shrwsn = sprintf("%05d-%05d", rand(100000), rand(100000));

> and all is cool, but i just noticed, that for the three entries i have made
> test wise:
>
> 79109-72626
> 106421-1023 these last two are six and four digits.....
> 108629-3542
>
> WHY? these were all made with the SAME script...............


--
Go to http://MarcDashevsky.com to send me e-mail.
 
Reply With Quote
 
 
 
 
daniel kaplan
Guest
Posts: n/a
 
      09-15-2004
i'll do that, but somehow the two OFF results don't ake sense to me
"Marc Dashevsky" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) t...
> In article <(E-Mail Removed)>, (E-Mail Removed) says...
> > so i generate a serial number with the exact code below (three lines):
> >
> > $number1 = 10000 + int(rand(99999));
> > $number2 = 10000 + int(rand(99999));
> > $shrwsn = "$number1-$number2";
> >
> > the point is i want TWO five digit numbers separated by a dash "-"

>
> So why don't you do:
>
> $shrwsn = sprintf("%05d-%05d", rand(100000), rand(100000));
>
> > and all is cool, but i just noticed, that for the three entries i have

made
> > test wise:
> >
> > 79109-72626
> > 106421-1023 these last two are six and four digits.....
> > 108629-3542
> >
> > WHY? these were all made with the SAME script...............

>
> --
> Go to http://MarcDashevsky.com to send me e-mail.



 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      09-15-2004
daniel kaplan wrote:

> so i generate a serial number with the exact code below (three lines):
>
> $number1 = 10000 + int(rand(99999));


That creates a number between 10000 and 109999, which can be 6 digits.

> 106421-1023 these last two are six and four digits.....
> 108629-3542


They look like a six digit number with a hyphen and a five or six
digit number where the result has been truncated to 11 characters.

$result = substr "106421-10230",0,11; # or "106421-102300"
$result = substr "108629-35429",0,11;

-Joe
 
Reply With Quote
 
Ian Sedwell
Guest
Posts: n/a
 
      09-21-2004
Hi Daniel

As a Pascal/C/Modula-2 compiled languages rule OK programmer, my gut
reaction is integer overflow. Perl performs all of its numerical
calculations using floating point math in a manner that is determined by the
CPU and the operating system on which it is running. This will obviously
vary from platform to platform. To be honest I doubt that this is in fact
the explanation and someone more knowledgeable than I will set you straight.

But there are two things that might help you home in on the problem.

First off, try the same routine, but with numbers in a range less than
32,000.

Second, have you tried using the integer pragma?

use integer;
#your integer arithmetic code goes here
no integer;
#back to using floating point


On a different note, if you want your numbers to be as close to random as
possible, you should set a random seed first. If you don't Perl is quite
likely to produce the same 'random' series each time you run it. So you
should certainly mod your code to be something like:

srand();
$randomNumber = int(rand(999));

You can provide a seed parameter to srand, but if you don't it will use the
current time.


Happy problem solving

Ian




On 2004/09/15 01:58, in article (E-Mail Removed), "daniel
kaplan" <(E-Mail Removed)> wrote:

> so i generate a serial number with the exact code below (three lines):
>
> $number1 = 10000 + int(rand(99999));
> $number2 = 10000 + int(rand(99999));
> $shrwsn = "$number1-$number2";
>
> the point is i want TWO five digit numbers separated by a dash "-"
> and all is cool, but i just noticed, that for the three entries i have made
> test wise:
>
> 79109-72626
> 106421-1023 these last two are six and four digits.....
> 108629-3542
>
> WHY? these were all made with the SAME script...............
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments