473,884 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5103
In article <10************ ***@nntp.acecap e.com>, no****@nospam.c om 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****@MarcDas hevsky.com> wrote in message
news:MP******** *************** *@netnews.comca st.net...
In article <10************ ***@nntp.acecap e.com>, no****@nospam.c om 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
4235
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 random number testsuite it passed all of the tests thrown at it for randomness. Can anyone suggest a test I could use that it would fail? A pointer to a suitable article would be appreciated. Thanks for your help in advance, Tom
36
7155
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, because they're written by people with skills far above mine. Hence, I've always used rand() and FALSELY assumed it could produce unpredictable random numbers and could be used in many situations even if security was an issue. Firstly, lets assume...
36
2705
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
1765
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
2805
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 my language simply propogates up to the rand( ) function from c++. For some reason, C++ will give each thread independent use of their own rand( ) function, so that a rand( ) call from one thread won't affect another thread's call to rand( )....
26
4117
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
3680
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 it always alternated between 2 values ? On the practical side do you have any thoughts on what one could realistically expect from the behaviour of rand() ? Could for example one expect that eventually any value in the range will be returned ?
10
3033
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
3954
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 of two (or near a power of two), that this is not an issue. The upper bits will participate equally in the result with the lower. Am I missing something? Thanks!
0
9953
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10868
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7984
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.