Re: Glib rand() revisited: RAND_MAX?
- From: Sebastian Wilhelmi <wilhelmi ira uka de>
- To: gtk-devel-list redhat com
- Subject: Re: Glib rand() revisited: RAND_MAX?
- Date: Tue, 22 Feb 2000 09:58:53 +0000
Hi Derek,
> Should g_rand_int() (in CVS) have an equivalent of RAND_MAX
> #defined? (it would just be the highest number a guint32 can hold)
As everybody knows the highest number a guint32 can hold is 2^32-1 I dont
think, we should have yet another constant for that. Other opinions?
> I have seen (and am trying to duplicate :) code that 'flips a
> coin', and it looks like this:
>
> if ( rand() < RAND_MAX/2 ) {
> side = heads;
> }
> else {
> side = tails;
> }
>
> Is there a cleaner way to choose a random TRUE/FALSE value? I
> was thinking of doing:
>
> g_rand_int_range(rand, 0, 1)
>
> ...but that really seems like overkill.
In the end it wont be that slower. But surly binary decision can be achieved
simpler:
if (g_rand_int(...) & 1<<number) true(); else false();
where number is an (almost) arbitrary constant between 0 and 31. The
distribution of the bits is said to be quite good for all bits for this random
number generator in general one should however use higher bits for such
things. Most of the rand() implementations for example alternate the last bit
IIRC. Not quite randomly...
Bye,
Sebastian
--
Sebastian Wilhelmi | här ovanför alla molnen
mailto:wilhelmi@ira.uka.de | är himmlen så förunderligt blå
http://goethe.ira.uka.de/~wilhelmi |
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]