473,666 Members | 2,102 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 5090
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
4211
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
7122
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
2678
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
1753
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
2791
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
4093
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
3656
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
3010
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
3931
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
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8876
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
8784
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...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1777
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.