473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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...............
Jul 19 '05 #1
4 5076
In article <10***************@nntp.acecape.com>, no****@nospam.com 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.
Jul 19 '05 #2
i'll do that, but somehow the two OFF results don't ake sense to me
"Marc Dashevsky" <us****@MarcDashevsky.com> wrote in message
news:MP************************@netnews.comcast.ne t...
In article <10***************@nntp.acecape.com>, no****@nospam.com 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.

Jul 19 '05 #3
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
Jul 19 '05 #4
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 10***************@nntp.acecape.com, "daniel
kaplan" <no****@nospam.com> 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...............


Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Tom McCallum | last post by:
Can any tell me why rand() is such a bad pseudo-random number generator. In all the articles I have read they say that you can predict the outcome of rand() but when I used its output with NIST's...
36
by: Ben Justice | last post by:
For a program in c, I need some random numbers for a system were people are placing bets. This is not a commerical project btw. Generally, I tend to rely on things from the standard library,...
36
by: Profetas | last post by:
Hi, I want to generate a random 8 bit number using rand(0 is that possible? to expecifu the base and the lenght? thanks
5
by: vib | last post by:
Hi there, i) Is there any other way to replace the rand()of standard C lib? ii) How efficient is the rand()? Thanks in advance. vib
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
26
by: Gary Wessle | last post by:
Hi I need help to generate some random numbers between 2 and 8. #include <cstdlib> using std::rand; the following was out of my range, int main() {
13
by: Spiros Bousbouras | last post by:
The standard says that rand() should return a pseudo-random number but what does pseudorandom mean ? If an implementation of rand() always returned the same number would it be conforming ? What if...
10
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.