 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
HUNGER Guest
|
Posted: Fri Aug 15, 2003 5:33 pm Post subject: RAND GENERATOR IN C++ |
|
|
HI,
I need to generate a long random number upto 4 byte width
but my compiler is generating 2 byte int type as RAND_MAX
i want to genetrate a rand num like 123456789 that long
plz help me.
thanks for helping
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Bell Guest
|
Posted: Sat Aug 16, 2003 7:56 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
[email]hunger_axel (AT) yahoo (DOT) com[/email] (HUNGER) wrote in message news:<f99f73d.0308150134.6df47e30 (AT) posting (DOT) google.com>...
| Quote: | HI,
I need to generate a long random number upto 4 byte width
but my compiler is generating 2 byte int type as RAND_MAX
i want to genetrate a rand num like 123456789 that long
plz help me.
thanks for helping
|
Check out the boost random number library.
http://www.boost.org
Bob
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sat Aug 16, 2003 8:14 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
"HUNGER" <hunger_axel (AT) yahoo (DOT) com> wrote in message
| Quote: | I need to generate a long random number upto 4 byte width
but my compiler is generating 2 byte int type as RAND_MAX
i want to genetrate a rand num like 123456789 that long
plz help me.
thanks for helping
|
On your system int is 2 bytes and long 4 bytes? In this case you can make
two random numbers and put them side by side.
long superrand() {
int x1 = rand();
int x2 = rand();
long x = x1;
x <<= CHAR_BITS * sizeof(int);
x |= x2;
return x;
}
But it's not portable. It is possible that long is also 2 bytes, or int and
long are 4 bytes, or int is 4 bytes and long is 8 bytes. I think this might
work, but could be wrong:
template
long dosuperrand() {
long x = dosuperrand<N-1>();
x <<= CHAR_BITS * sizeof(int);
int x2 = rand();
x |= x2;
return x;
}
template <>
long dosuperrand<1>() {
int x1 = rand();
return x1;
}
template <>
long dosuperrand<2>() {
int x1 = rand();
int x2 = rand();
long x = x1;
x <<= CHAR_BITS * sizeof(int);
x |= x2;
return x;
}
long superrand() {
// assuming sizeof(long)%sizeof(int) is zero
return dosuperrand
}
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thorsten Ottosen Guest
|
Posted: Sat Aug 16, 2003 8:29 pm Post subject: Re: RAND GENERATOR IN C++ |
|
|
"HUNGER" <hunger_axel (AT) yahoo (DOT) com> wrote
| Quote: | HI,
I need to generate a long random number upto 4 byte width
but my compiler is generating 2 byte int type as RAND_MAX
i want to genetrate a rand num like 123456789 that long
plz help me.
|
the normal std::rand() is rarely very random.
a very good overview can be found in "Numerical Recipes in C++, Second.
Edition"
cheers
Thorsten, AAU
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sun Aug 17, 2003 1:38 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
Thorsten Ottosen wrote:
| Quote: |
the normal std::rand() is rarely very random.
|
That is the usual statement. Repetition is not truth, however.
| Quote: |
a very good overview can be found in "Numerical Recipes in C++, Second.
Edition"
|
It's not a good overview. It's superficial and snide.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Liddicott Guest
|
Posted: Sun Aug 17, 2003 1:42 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
Anyone considering using a numerical method to generate random numbers, is, of course, in a state of sin.
(Knuth)
Or you can use
inline long rand_long(){return long(rand()) << 16 & rand();};
--
Cheers,
Ben Liddicott
"HUNGER"
| Quote: | I need to generate a long random number upto 4 byte width
but my compiler is generating 2 byte int type as RAND_MAX
i want to genetrate a rand num like 123456789 that long
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thorsten Ottosen Guest
|
Posted: Sun Aug 17, 2003 11:45 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
"Pete Becker" <petebecker (AT) acm (DOT) org> wrote
| Quote: | Thorsten Ottosen wrote:
the normal std::rand() is rarely very random.
That is the usual statement. Repetition is not truth, however.
|
I agree about "repetition is not truth". On which compilers would you
consider std.:rand() good?.
| Quote: |
a very good overview can be found in "Numerical Recipes in C++, Second.
Edition"
It's not a good overview. It's superficial and snide.
|
I'm glad to hear that...then I can start unlearning what I've learnt.
Could you explain why it's snide?
Thanks
Thorsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sun Aug 17, 2003 4:35 pm Post subject: Re: RAND GENERATOR IN C++ |
|
|
Thorsten Ottosen wrote:
| Quote: |
"Pete Becker" <petebecker (AT) acm (DOT) org> wrote in message
news:3F3E9C10.A576D02F (AT) acm (DOT) org...
Thorsten Ottosen wrote:
the normal std::rand() is rarely very random.
That is the usual statement. Repetition is not truth, however.
I agree about "repetition is not truth". On which compilers would you
consider std.:rand() good?.
|
At least every one that uses our library. But that's not the issue. You
said they're bad. If you have data to back that up, say so.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sun Aug 17, 2003 4:36 pm Post subject: Re: RAND GENERATOR IN C++ |
|
|
John Potter wrote:
| Quote: |
On 16 Aug 2003 21:42:34 -0400, "Ben Liddicott"
[email]ben.liddicott (AT) bencat (DOT) demon.co.uk[/email]> wrote:
Or you can use
inline long rand_long(){return long(rand()) << 16 & rand();};
You, of course, meant | not &. This produces half as many not as
random numbers since it uses two calls to rand.
|
Well, not really. Every generator provides some number of bits, n,
between repetitions. If you use those bits 16 at a time you get n/16
values before you hit repetitions from the generator. If you use those
same bits 32 at a time instead you get n/32 values, i.e. half as many,
before you hit repetitions from the generator. It doesn't matter whether
the generator returns 16-bit values or 32-bit values. What matters is
the period of the generator. For example:
static unsigned next_value;
bool valid = false;
unsigned long rand32(); // returns 32-bit "random" values
unsigned rand16()
{
if (!valid)
{
unsigned long val = rand32();
next_value = val >> 16;
valid = true;
return val & 0xffff;
}
else
{
valid = false;
return next_value;
}
}
Now, if you generate 32-bit values by calling rand16 twice you're just
reassembling the values that rand16 splits apart from calls to rand32.
So you get exactly the same 32-bit values either way.
Part of assessing the quality of a random number generator is looking at
the size of its internal state. The number of bits in the value that it
returns is irrelevant.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun Aug 17, 2003 4:38 pm Post subject: Re: RAND GENERATOR IN C++ |
|
|
In article <3f3f28e7$0$14561$afc38c87 (AT) news (DOT) optusnet.com.au>, Thorsten
Ottosen <nesotto (AT) cs (DOT) auc.dk> writes
| Quote: | It's not a good overview. It's superficial and snide.
I'm glad to hear that...then I can start unlearning what I've learnt.
Could you explain why it's snide?
|
I do not know about snide but I do know that the intellectual property
right claims made by the authors of this book make it a very dangerous
book for professional programers to be on record as having read
http://www.numerical-recipes.com/com/screen-license.html
This is without doubt the most draconian license that I have ever read.
And the single screen license actually is as expensive as the book
(actually much more so if you use Linux)
This is one of two books that I recommend that professional programmers
should NOT buy because the license agreement makes it prejudicial to
your continued employment as a software developer.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Mon Aug 18, 2003 10:35 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
"Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) att.net> wrote
| Quote: | But it's not portable. It is possible that long is also 2 bytes, or int
and
long are 4 bytes, or int is 4 bytes and long is 8 bytes. I think this
might
work, but could be wrong:
long superrand() {
// assuming sizeof(long)%sizeof(int) is zero
return dosuperrand<sizeof(long)/sizeof(int)>();
}
|
Oops. In the issue of portability I've made a big omission. Sorry! Namely
you want a 32 bit random number, and we don't know which type is 32 bit.
Sure it's long for you, but it's int for me.
So you want something like this
#ifdef 16bitinteger /* use proper macro */
typedef long int32;
#elif 32bitinteger /* as above */
typedef int int32;
#endif
The change the signature of function superrand() to
int32 rand32();
// assuming sizeof(int32)%sizeof(int) is zero
return dosuperrand<sizeof(int32)/sizeof(int)>();
// oops: need to handle special case when sizeof(int32)/sizeof(int) is
zero
}
To get the int32 you can use the following template trick. But it works
only if int32 is actually one of the basic types. If not, you have to use
std::bitset
template <bool cond, class A, class B>
struct template_if
{
};
template <class A, class B>
struct template_if<true,A,B>
{
typedef A result;
};
template <class A, class B>
struct template_if<false,A,B>
{
typedef B result;
};
////////////////////////////////////////////////////////////////////////////
////
template <class T, class U=void>
struct typelist
{
typedef T car;
typedef U cdr;
};
template <template
struct typelist_find
{
typedef typename list::car car;
typedef typename list::cdr cdr;
typedef typename
template_if<
Predicate
car,
typename find_typelist<Predicate,cdr>::result
};
template <template
struct typelist_find<Predicate,void>
{
typedef void result;
};
const int CHAR_BITS=8;
template <class T>
struct Eq32
{
static const bool result=((sizeof(T)*CHAR_BITS)==32);
};
typedef typelist<long, typelist > >
BasicTypes;
typedef find_typelist<Eq32, BasicTypes>::result int32;
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Mon Aug 18, 2003 10:36 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
"Ben Liddicott" <ben.liddicott (AT) bencat (DOT) demon.co.uk> wrote in message
| Quote: | Or you can use
inline long rand_long(){return long(rand()) << 16 & rand();};
|
Should that be & or |
In addition, people may be confused how operators group in some cases, and
I'm one of them here . So I suggest using parenthesis
inline long rand_long(){return (long(rand()) << 16) & rand();};
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thorsten Ottosen Guest
|
Posted: Mon Aug 18, 2003 11:56 pm Post subject: Re: RAND GENERATOR IN C++ |
|
|
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In article <3f3f28e7$0$14561$afc38c87 (AT) news (DOT) optusnet.com.au>, Thorsten
Ottosen <nesotto (AT) cs (DOT) auc.dk> writes
It's not a good overview. It's superficial and snide.
I'm glad to hear that...then I can start unlearning what I've learnt.
Could you explain why it's snide?
I do not know about snide but I do know that the intellectual property
right claims made by the authors of this book make it a very dangerous
book for professional programers to be on record as having read
http://www.numerical-recipes.com/com/screen-license.html
|
this is very enjoyable reading. I have never reda it before, and I most say
its
amazing, thay can wrute that.
| Quote: | This is without doubt the most draconian license that I have ever read.
And the single screen license actually is as expensive as the book
(actually much more so if you use Linux)
This is one of two books that I recommend that professional programmers
should NOT buy because the license agreement makes it prejudicial to
your continued employment as a software developer.
|
I woldn't worry that much. I still think the book is good regarding the math
topics and that was why I
recommended it.
On the other hand, even without reading the license, I would never dream
about
using the source code. It's very old-fashioned and full of low level
code...not mmuch C++ in that.
Numbers are hardcode in great numbers too, no code is resued in functions,
but put inline again etc.
best regards
Thorsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
AngleWyrm Guest
|
Posted: Tue Aug 19, 2003 12:31 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
"Thorsten Ottosen" <nesotto (AT) cs (DOT) auc.dk> wrote
| Quote: |
"Pete Becker" <petebecker (AT) acm (DOT) org> wrote in message
news:3F3E9C10.A576D02F (AT) acm (DOT) org...
Thorsten Ottosen wrote:
the normal std::rand() is rarely very random.
That is the usual statement. Repetition is not truth, however.
I agree about "repetition is not truth". On which compilers would you
consider std.:rand() good?.
|
To clear things up a bit: std::rand() is a Linear Congruential pseudo-random number generator; it
uses a formula of the form:
output = (lastOutput+constant) modulo anotherConstant.
The caution expressed has to do with a couple generally undesirable traits of LC generators.
1). Don't use them in large groups to generate vectors (n-tuples) because they are not sequentially
random--the points they generate fall into planes. That is to say, if a vector of numDimensions is
created and filled with numDimension random numbers, all points will lie in a numDimension-1 plane.
2). The low-order bits are not random, and this can become a problem during use. Some care in
converting a rand() to a range is required. Here is an example of a reasonable method of doing so:
// Generate a random integer, given the number of possibilities
static bool srandCalled=false;
int RandomInt(const int numChoices) // for numChoices<=RAND_MAX; else returns 0
{
if( !srandCalled ){ // make sure that srand has been called
srand( time(NULL) );
rand(); // burn one for good measure
srandCalled = true;
}
// Divide RAND_MAX into numChoices buckets
const int bucketSize = RAND_MAX / numChoices;
static int randomInteger;
do{
randomInteger = rand() / bucketSize; // Get a random bucket
}
while ( randomInteger >= numChoices ); // re-roll the remainder bucket
return randomInteger;
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Tue Aug 19, 2003 10:26 am Post subject: Re: RAND GENERATOR IN C++ |
|
|
AngleWyrm wrote:
| Quote: |
To clear things up a bit: std::rand() is a Linear Congruential pseudo-random number generator;
|
There is no such requirement.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|