473,418 Members | 5,099 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,418 software developers and data experts.

generater big rand number

i use g++ to generater rand number, now i find that the RAND_MAX is
32367 in my computer, how can i make a bigger rand number( the number
is wihin in the integer(2^32-1))
Oct 19 '08 #1
8 4702
On 19 Okt., 09:37, remlostime <remlost...@gmail.comwrote:
i use g++ to generater rand number, now i find that the RAND_MAX is
32367 in my computer, how can i make a bigger rand number( the number
is wihin in the integer(2^32-1))
You have several options: one is to use a library such as boost which
gives you several options to choose different random number generators
of high quality. Another is to compose your number by calling rand
several times. To illustrate: if your random number only gave values
from 0 to 9, but you needed values from 0 to 99, the solution would my
to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)

/Peter
Oct 19 '08 #2
remlostime wrote:
i use g++ to generater rand number, now i find that the RAND_MAX is
32367 in my computer, how can i make a bigger rand number( the number
is wihin in the integer(2^32-1))
By using a better random number generator? There are tons of them if
you search the internet.

(Curiously it's actually surprisingly difficult to find a good random
number generator which is portable, written in C++, very easy to use and
doesn't require you to install the entire Boost library to simply use
the RNG. For this reason I made this C++ version of the ISAAC rng:
http://warp.povusers.org/IsaacRand.zip )
Oct 19 '08 #3
On 19 Oct, 10:43, peter koch <peter.koch.lar...@gmail.comwrote:
On 19 Okt., 09:37, remlostime <remlost...@gmail.comwrote:
i use g++ to generater rand number, now i find that the RAND_MAX is
32367 in my computer, how can i make a bigger rand number( the number
is wihin in the integer(2^32-1))

You have several options: one is to use a library such as boost which
gives you several options to choose different random number generators
of high quality. Another is to compose your number by calling rand
several times. To illustrate: if your random number only gave values
from 0 to 9, but you needed values from 0 to 99, the solution would my
to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)
I don't think your second approach will work. For example, suppose
your random number generator gives only ten values and produces the
sequence:

1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc

then your improved version will only ever give the numbers 16, 64, 47,
79, 90, 3, 35, 52, 28 and 81, instead of the full range from 0 to 99.
Oct 19 '08 #4
In article <9L*************@read4.inet.fi>, Juha Nieminen
<no****@thanks.invalidwrote:
(Curiously it's actually surprisingly difficult to find a good random
number generator which is portable, written in C++, very easy to use and
doesn't require you to install the entire Boost library to simply use
the RNG. For this reason I made this C++ version of the ISAAC rng:
http://warp.povusers.org/IsaacRand.zip )
Agreed; that one seems to assume that unsigned int has exactly 32 bits, no
more, no less. Take a look at the ind macro in IsaacRand.cc line 21.
That's just one non-portability I found after a quick scan.
Oct 19 '08 #5
On 19 Okt., 22:03, gw7...@aol.com wrote:
On 19 Oct, 10:43, peter koch <peter.koch.lar...@gmail.comwrote:
On 19 Okt., 09:37, remlostime <remlost...@gmail.comwrote:
i use g++ to generater rand number, now i find that the RAND_MAX is
32367 in my computer, how can i make a bigger rand number( the number
is wihin in the integer(2^32-1))
You have several options: one is to use a library such as boost which
gives you several options to choose different random number generators
of high quality. Another is to compose your number by calling rand
several times. To illustrate: if your random number only gave values
from 0 to 9, but you needed values from 0 to 99, the solution would my
to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)

I don't think your second approach will work. For example, suppose
your random number generator gives only ten values and produces the
sequence:

1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc

then your improved version will only ever give the numbers 16, 64, 47,
79, 90, 3, 35, 52, 28 and 81, instead of the full range from 0 to 99.
That would be a short sequence - not anything you'd expect from a
quality random generator.

/Peter
Oct 19 '08 #6
On 2008-10-19 16:03:39 -0400, gw****@aol.com said:
On 19 Oct, 10:43, peter koch <peter.koch.lar...@gmail.comwrote:
>On 19 Okt., 09:37, remlostime <remlost...@gmail.comwrote:
>>i use g++ to generater rand number, now i find that the RAND_MAX is
32367 in my computer, how can i make a bigger rand number( the number
is wihin in the integer(2^32-1))

You have several options: one is to use a library such as boost which
gives you several options to choose different random number generators
of high quality. Another is to compose your number by calling rand
several times. To illustrate: if your random number only gave values
from 0 to 9, but you needed values from 0 to 99, the solution would my
to use rand()*10 + rand(). I am sure you can extrapolate from here ;-)

I don't think your second approach will work. For example, suppose
your random number generator gives only ten values and produces the
sequence:

1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc

then your improved version will only ever give the numbers 16, 64, 47,
79, 90, 3, 35, 52, 28 and 81, instead of the full range from 0 to 99.
Well, yes, if the generator produces a sequence of length ten, then
there's not much you can do. But producing random digits from 0 to 9
doesn't mean producing a sequence of length ten. Typically a random
number generator has a much longer period than that. If the period is
long enough, there's no problem tiling the values, as the second
approach suggests.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 19 '08 #7
On Oct 19, 11:26 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-10-19 16:03:39 -0400, gw7...@aol.com said:
On 19 Oct, 10:43, peter koch <peter.koch.lar...@gmail.comwrote:
On 19 Okt., 09:37, remlostime <remlost...@gmail.comwrote:
>i use g++ to generater rand number, now i find that the
RAND_MAX is 32367 in my computer, how can i make a bigger
rand number( the number is wihin in the integer(2^32-1))
You have several options: one is to use a library such as
boost which gives you several options to choose different
random number generators of high quality. Another is to
compose your number by calling rand several times. To
illustrate: if your random number only gave values from 0
to 9, but you needed values from 0 to 99, the solution
would my to use rand()*10 + rand(). I am sure you can
extrapolate from here ;-)
I don't think your second approach will work. For example,
suppose your random number generator gives only ten values
and produces the sequence:
1, 6, 4, 7, 9, 0, 3, 5, 2, 8, 1, 6, etc
then your improved version will only ever give the numbers
16, 64, 47, 79, 90, 3, 35, 52, 28 and 81, instead of the
full range from 0 to 99.
Well, yes, if the generator produces a sequence of length ten,
then there's not much you can do. But producing random digits
from 0 to 9 doesn't mean producing a sequence of length ten.
Typically a random number generator has a much longer period
than that. If the period is long enough, there's no problem
tiling the values, as the second approach suggests.
Yes and no. The sequence should be significantly longer than
the value one is trying to generate; in other words, the value
actually returned by rand() shouldn't represent the entire
internal state of the machine.

For historical reasons, at in some environments, RAND_MAX is
defined as 32767, even though the actual generator uses 31 or 32
bits internally (and has an actual period of around 2^31). If
this is the case, then the proposed technique is fine.
Similarly, rand() is required to return an int---a number of
quality generators use significantly more state internally. In
such cases, the technique is also valid. If the generator's
period is only RAND_MAX, however, it's likely to be too pretty
bad (but depending on the use, maybe "good enough" anyway).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 20 '08 #8
blargg wrote:
In article <9L*************@read4.inet.fi>, Juha Nieminen
<no****@thanks.invalidwrote:
> (Curiously it's actually surprisingly difficult to find a good random
number generator which is portable, written in C++, very easy to use and
doesn't require you to install the entire Boost library to simply use
the RNG. For this reason I made this C++ version of the ISAAC rng:
http://warp.povusers.org/IsaacRand.zip )

Agreed; that one seems to assume that unsigned int has exactly 32 bits, no
more, no less. Take a look at the ind macro in IsaacRand.cc line 21.
That's just one non-portability I found after a quick scan.
When I say "portable" I mean things like "doesn't include <windows.h>"
and the like. I have seen *way* too many RNGs out there which use
whatever non-standard headers and non-standard code.

If you are worried that in some system 'int' might not be 32-bit then
put some assert() somewhere or whatever. The actual RNG code is not
mine. I only wrapped it inside the class and removed everything from the
global namespace (the original C code put something like 100 symbols in
the global namespace). It fulfills my needs for a fast high-quality RNG
in both linux and windows just fine. The same cannot be said from the
majority of RNG libraries I have seen.
Oct 20 '08 #9

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...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
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
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
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...
5
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
10
by: Fred | last post by:
Hi guys, I was wondering how could i get a random number exemple between 1 and 100 but with n% of getting some value over a number x. Thanks a lot in advance. Fred
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.