Berkeley CSUA MOTD:Entry 27308
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/24 [General] UID:1000 Activity:popular
5/24    

2003/2/5-6 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Languages/Java] UID:27308 Activity:high
2/5     In C, the typical way to generate random numbers in the range [0, N)
        is (int) ((double) rand() / ((double) RAND_MAX + 1) * N).  What
        guarantee is there that RAND_MAX + 1 does not overflow?
        \_ RAND_MAX is 32767.
           \_ RAND_MAX is guaranteed to be at LEAST 32767.
        \_ aren't your parentheses misplaced wrt N?
           \_ no.  You generate a floating value in the range [0, 1), multiply
              by N, and cast to int.
              \_ right. as written, it's generating [0,1) and dividing by N,
                 n'est-ce pas?
        \_ Note that you're casting RAND_MAX to a double.  Standard IEEE754 has
           54 bits of significand in a double.  That's typically more than
           anything a 32-bit machine has to offer.  However there is no
           guarantee against overflow.
           By the way, rand() is a crappy generator.  The method above leads to
           non-uniform probability distribution (some values will be twice as
           likely than the rest), etc.
                \_ so can you change the seed each time, based on prev #
                   and other factors to make it distribute more evenly?
                   \_ it doesn't matter what the seed is.  most implementations
                      of rand() just suck.
                      \_ understood.  care to suggest a better way of
                         generating random numbers?
                         \_ digitize johnson noise and take the least
                            signifigant bit?  i'll bet you could make
                            a box to do this for about a dollar in electronics.
                            I think there are chips that do this, or use
                            shot noise which is also white.
                            -naive physicist
                                \_ Actually, the best way would be to
                                   to take some radioactive material
                          \_ well even just C's "random" function is better than
                            "rand". look at "man 3 rand". i would try the /dev
                            random device but then that's not very portable
                                   and measure either the amount of
                                   decay or the interval between two
                                   decay events and use that as a
                                   basis for your random numbers.
                          \_ read from /dev/(u)random
                          \_ http://www.boost.org/libs/random/index.html
                          \_ well even just C's "random" function is better
                             than "rand". look at "man 3 rand". i would try
                             the /dev random device but then that's not very
                             portable
                                \_ It works on MacOS X, Solaris (8+),
                                   L1NUX, *BSD, how much more portability
                                   do you want?
        \_ (int) (drand48() * N);
2025/05/24 [General] UID:1000 Activity:popular
5/24    

You may also be interested in these entries...
2005/1/21-22 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Languages/Java] UID:35849 Activity:high
1/21   Thought this was an interesting read:
       http://paulgraham.com/noop.html
       I tend to agree with him, as I've seen relatively little gain
       from such languages as C++ and Java in terms of creating better
       software.
        \_ I thought it was pretty sophomoric, really.  OOP is a tool that is
	...
2004/10/29-30 [Computer/SW/Languages/C_Cplusplus] UID:34449 Activity:very high
10/29   C++ is so freaking BROKEN.  Augh!
        \_ Just use C.
           \_ Would if I could.
        \_ No, you are.  C++ works just fine, and far better than C for many
           purposes.
           \_ C vs. C++.  FIGHT!!!
	...
2004/8/18-19 [Computer/SW/Languages/C_Cplusplus] UID:32995 Activity:very high
8/18    Doesn't math.h define min() and max() functions?  What should I include
        to get them?  I'd rather not do the (a < b) ? a : b thing.  Thx
        \_ No.  Many programmers define it as a new macro.  Don't know what
           gcc people do, offhand.
        \_ OS X has fmin(3)/fmax(3) which conform to ISO/IEC 9899:1999(E).
           Linux might have these too.
	...
2004/3/30-31 [Computer/SW/Languages/Perl] UID:12925 Activity:kinda low
3/17    In Perl, how do I make variables have static types and type check
        for valid parameter/actuals? I realize that variables are untyped
        in Perl ($var can be 0.001 or "hello") but I'd like to have more
        strict checking so that errors would be caught ahead of run-time,
        Thanks,                                                 -java guy
        \_ use java.  Seriously.  You don't use perl if you want strong
	...
2003/7/6-7 [Computer/SW/Languages/C_Cplusplus] UID:28939 Activity:high
7/5     Besides method lookup of non-virtual methods, how is C++ considered
        slower than pure C?  The follow-up question: why hasn't C++ or
        another OO language moved into usage in kernels and drivers?
        \_ Larger standard library + linkers which link everything =
        \_ You probably mean "Besides method lookup of virtual methods".
           large code = poor cache performance.
	...
2003/4/22-23 [Computer/SW/Languages/C_Cplusplus] UID:28189 Activity:insanely high
4/22    Anyone know a good link that explains all of C++'s use of the
        keyword mconst?
        \_ http://www.parashift.com/c++-faq-lite
           Search for const in the text box.
           Search for mconst in the text box.
           \- perfection
	...
2002/11/5 [Computer/SW/Languages/C_Cplusplus] UID:26409 Activity:high
11/4    I'm having a problem formatting inline elements in xsl-- I have to
        handle the situation where
        <school>I graduated from
          <link href="<DEAD>www.berkeley.edu"<DEAD>Cal</link> in 1998.
        </school>
        My current template is like this:
	...
2001/3/17-18 [Computer/SW/Languages/C_Cplusplus] UID:20827 Activity:high
3/16    Why does so much C sample code use #define instead of const?
        \_ because any good C code will use a bunch of preprocessor
           anyways. you can't be a good C programmer and eschew the
           preprocessor. For that, you need a language which fills those
           gaps with other constructs (c++ templates go a long way to
           obviate the need for preprocessor for example). you
	...
2000/8/9-10 [Academia/Berkeley/Classes, Computer/SW/Languages/C_Cplusplus] UID:18929 Activity:high
8/8     How well do UCB EECS and CS newgrads know C/C++? (in general)
        \_ If it makes you feel any better, Stanford undergrads start out
           take 3 quarters of "Introductory Programming" in C instead of
           SICP.
        \_ on average, better than other schools' new grads, with a
           lot more upside
	...
2000/4/6-7 [Computer/SW/Languages/C_Cplusplus] UID:17942 Activity:very high
4/6     C vs. Java vs. Perl comparisons:
        Conclusion: C is still the fastest. Java is not as slow as people
        think it is. Perl is nowhere close to Java performance.
        \_ Apples, Oranges, Bananas, and Trolls who like to delete
                \_ Multithreaded java with native threads with a
                   decent rt gives you functionality that you can
	...
Cache (1878 bytes)
www.boost.org/libs/random/index.html
In order to map the interface of the generators and distribution functions to other concepts, some 33 decorators are available. Rationale The methods for generating and evaluating deterministic and non-deterministic random numbers differ radically. Furthermore, due to the inherent deterministic design of present-day computers, it is often difficult to implement non-deterministic random number generation facilities. Thus, the random number library is split into separate header files, mirroring the two different application domains. History and Acknowledgements In November 1999, Jeet Sukumaran proposed a framework based on virtual functions, and later sketched a template-based approach. Ed Brey pointed out that Microsoft Visual C++ does not support in-class member initializations and suggested the enum workaround. The first public release of this random number library materialized in March 2000 after extensive discussions on the boost mailing list. Many thanks to Beman Dawes for his original min_rand class, portability fixes, documentation suggestions, and general guidance. Harry Erwin sent a header file which provided additional insight into the requirements. Ed Brey and Beman Dawes wanted an iterator-like interface. Beman Dawes managed the formal review, during which Matthias Troyer, Csaba Szepesvari, and Thomas Holenstein gave detailed comments. The reviewed version became an official part of boost on 17 June 2000. Gary Powell contributed suggestions for code cleanliness. Dave Abrahams and Howard Hinnant suggested to move the basic generator templates from namespace boost::detail to boost::random. Ed Brey asked to remove superfluous warnings and helped with uint64_t handling. Matthias Troyer contributed a lagged Fibonacci generator. Michael Stevens found a bug in the copy semantics of normal_distribution and suggested documentation improvements.