# rand() question

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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...............

## Re: rand() question

[crosspost to defunct group comp.lang.perl removed]

daniel kaplan 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...............

The last time I checked, 90,000 + 10,000 = 100,000 and that's a 6 digit
number.  Please check the perl FAQ:
perldoc random
(2nd question returned)

As for the four digit number, you'll have to show us how you're printing
it out.  Where is this string being stored, and how is it being
retrieved for your viewing?  (My initial guess is that you're storing it
in a varchar database field that has a length of 10, but that's just a
guess).

Paul Lalli

## Re: rand() question

Paul Lalli wrote:

> Please check the perl FAQ:
> perldoc random
> (2nd question returned)

That should, of course, be:
perldoc -q random

Paul Lalli

## Re: rand() question

daniel kaplan 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 "-"

That's not what you're asking for with this code. You're asking for two
numbers from 10000 to 109999, inclusive.

What you want to do is as another poster suggested, use rand(100000) to
get a number from 0 to 99999, inclusive, and then use sprintf() to pad
that with zeroes as needed to get five digits.

> 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...............

The code you've given cannot produce either 1023 or 3542, so the problem
lies elsewhere, when \$shrwsn is stored and/or output.

Are you truncating these somewhere, using code that assumes that both
parts are five digits? Storing them in a fixed-length database column
perhaps?

By the way, comp.lang.perl is an ex-group. It's gone, defunct, pining
for the fjords. It's only standing up because your provider nailed it to
the server. It's *dead*. Followups set.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

## Re: rand() question

Sherm Pendley wrote:

>
> That's not what you're asking for with this code. You're asking for two
> numbers from 10000 to 109999, inclusive.
>

Actually it's 10_000 to 109_998 inclusive.

--- Shawn

## Re: rand() question

> 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...............

int(rand(99999)) can generate the integers greater than 90000 which
when added to 10000 will give numbers greater than 100000 which have
six digits.

As for the four digit part I can only assume you are only printing
(or keeping) the first 11 characters in the string and hence
truncating the last digit (or two) of the second number.

--
Sam Holden

## Re: rand() question

> 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.

## Re: rand() question

i'll do that, but somehow the two OFF results don't ake sense to me
> > 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
> > 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.

## Re: rand() question

On Tue, 14 Sep 2004 21:31:45 -0400, "daniel kaplan"

>i'll do that, but somehow the two OFF results don't ake sense to me

(ii) You'd better *not* post to clp.

(iii) What are the two "OFF results"?!?

Michele
--
->(map substr
((\$a||=join'',map--\$|x\$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,\$_,
256),7,249);s/[^\w,]/ /g;\$ \=/^J/?\$/:"\r";print,redo}#JAPH,

## Re: rand() question

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

## Re: rand() question

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 1095209944.109086@nntp.acecape.com, "daniel

> 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...............
>
>

## Re: rand() question

> On 2004/09/15 01:58, in article 1095209944.109086@nntp.acecape.com,
"daniel
>
> > 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
> > test wise:
> >
> > 79109-72626
> > 106421-1023        these last two are six and four digits.....
> > 108629-3542
>
> As a Pascal/C/Modula-2 compiled languages rule OK programmer, my gut
> reaction is integer overflow.

This has nothing to do with the problem.  Please read the complete
thread from a few weeks ago.

> 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.

This is wholly untrue for any Perl version greater than 5.004.  From
perldoc -f srand:
If srand() is not called explicitly, it is called
implicitly at the first use of the "rand" operator.

>So you should certainly mod your code to be something like:
> srand();
> \$randomNumber = int(rand(999));

If and only if you're using an obscenely outdated version of Perl.  And
if you are, you should be upgrading.

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

Also untrue.  Again from perldoc -f srand:
the generally acceptable default,
which is based on time of day, process ID, and
memory allocation, or the /dev/urandom device, if
available.

While obviously all attempts at helping are appreciated in this
discussion, it may behoove you to find a news reader which will show you
when a question has been answered rather completely weeks ago.  Also,