rand() question

Discussion in 'Perl' started by daniel kaplan, Sep 15, 2004.

  1. 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...............
     
    daniel kaplan, Sep 15, 2004
    #1
    1. Advertising

  2. In article <>, 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.
     
    Marc Dashevsky, Sep 15, 2004
    #2
    1. Advertising

  3. i'll do that, but somehow the two OFF results don't ake sense to me
    "Marc Dashevsky" <> wrote in message
    news:...
    > In article <>, 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.
     
    daniel kaplan, Sep 15, 2004
    #3
  4. daniel kaplan

    Joe Smith Guest

    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
     
    Joe Smith, Sep 15, 2004
    #4
  5. daniel kaplan

    Ian Sedwell Guest

    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 , "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...............
    >
    >
     
    Ian Sedwell, Sep 21, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Niko D. Barli

    rand function in Modelsim 5.7c

    Niko D. Barli, Aug 26, 2004, in forum: VHDL
    Replies:
    9
    Views:
    6,324
    Niko D. Barli
    Sep 6, 2004
  2. hedylogus

    Beginner Question--rand( )

    hedylogus, Jul 25, 2003, in forum: C++
    Replies:
    23
    Views:
    750
    Cy Edmunds
    Jul 28, 2003
  3. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    235
    Morton Goldberg
    Sep 16, 2007
  4. Panagiotis Atmatzidis

    Question abour rand()

    Panagiotis Atmatzidis, Dec 29, 2009, in forum: Ruby
    Replies:
    6
    Views:
    137
    Brian Candler
    Dec 29, 2009
  5. Marek Stepanek
    Replies:
    8
    Views:
    120
    Marek Stepanek
    Aug 16, 2006
Loading...

Share This Page