473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4721
On 19 Okt., 09:37, remlostime <remlost...@gma il.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.comwr ote:
On 19 Okt., 09:37, remlostime <remlost...@gma il.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.comwr ote:
On 19 Okt., 09:37, remlostime <remlost...@gma il.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.comwr ote:
>On 19 Okt., 09:37, remlostime <remlost...@gma il.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...@versatile coding.comwrote :
On 2008-10-19 16:03:39 -0400, gw7...@aol.com said:
On 19 Oct, 10:43, peter koch <peter.koch.lar ...@gmail.comwr ote:
On 19 Okt., 09:37, remlostime <remlost...@gma il.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 objektorientier ter Datenverarbeitu ng
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
4208
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
7
3239
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 library, which is based on GTK. Depending on which libraries I link in, I can have either a GTK1 or GTK2 user interface. I'm finding that the simulation behaves differently depending on whether I link with GTK1 or GTK2.
36
7120
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
2673
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
4384
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 500,000 consecutive rand calls then is the rand() algorithm going to guarantee me that no floating window of calls over that 500,000 rand calls will have the same value twice? The only way that would work is if the series was identical everytime.
26
4090
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 ?
5
2313
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 the same seed for reproducibility of the results. Now I am porting this (old C89) code and have setup a nice app with threads that drives on one thread the old code and on another the new code, so that I can compare the results and see that nothing...
10
2088
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
8199
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
8705
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
8505
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...
0
7196
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
5574
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
4092
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...
1
2626
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
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
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.