Thread: Is there any function better than rand()

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    4

    Is there any function better than rand()

    Is there any function better than rand() that can still get random numbers? If there isn't one, can i make my own in any way?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66
    There are lots and lots

    Try: http://www.agner.org/random/ as a start.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Theres this one, for C though (barely any C only code though).

    http://thorkildsen.no/faqsys/docs/random.c //Labelled the "best" method, but it was written a while ago so who knows.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Quote Originally Posted by Dae
    Theres this one, for C though (barely any C only code though).

    http://thorkildsen.no/faqsys/docs/random.c //Labelled the "best" method, but it was written a while ago so who knows.
    I dont know if it's the best method. But I do know the author is a void mainer.

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    First you have to define what you mean by 'good' so you can then evaluate 'better'. What are you going to use the random numbers for? The criteria for cryptography is much different than that for long-running simulations.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    In 99% of situations, rand() is good-enough if you seed it with time. All you need is a number that's unpredictiable. rand() is unpredictible enough for most games and simulations. You would generally need a large number of samples before you start to see a pattern or a trend. ...If you're gambling, you'll run out of money long before you figure-out how to out-smart rand()!

    Before you try to improve-on rand() you should determine what it is about rand() that needs improving. And, your version should not have weaknesses or limitations that are not present in rand().

    Just for fun... from the C language standard: *
    7.20.2 Pseudo-random sequence generation functions

    7.20.2.1 The rand function

    Synopsis
    1
    Code:
    #include <stdlib.h>
    int rand(void);
    Description
    2 The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.

    3 The implementation shall behave as if no library function calls the rand function.

    Returns
    4 The rand function returns a pseudo-random integer.

    Environmental limits
    5 The value of the RAND_MAX macro shall be at least 32767.

    7.20.2.2 The srand function

    Synopsis
    1
    Code:
    #include <stdlib.h>
    void srand(unsigned int seed);
    Description
    2 The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

    3 The implementation shall behave as if no library function calls the srand function.

    Returns
    4 The srand function returns no value.

    5 EXAMPLE The following functions define a portable implementation of rand and srand.
    Code:
    static unsigned long int next = 1;
    int rand(void) // RAND_MAX assumed to be 32767
    {
         next = next * 1103515245 + 12345;
         return (unsigned int)(next/65536) % 32768;
    }
    
    void srand(unsigned int seed)
    {
         next = seed;
    }
    *The C++ standard simply refers-back to the C standard.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Not to be the one to toot another member's horn, but this is a good read:

    http://eternallyconfuzzled.com/articles/rand.html

    and here is their rand library:

    http://eternallyconfuzzled.com/libs/jsw_rand.html
    Sent from my iPadŽ

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    MTwister is fast and has a period of 2^19937-1
    http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Is there any function better than rand() that can still get random numbers?
    I work in the encryption department for NORAD as a NATO liason, and I use this random number generator:
    Code:
    cout<<"Enter a number: ";
    
    int randNum=0;
    cin>>randNum;
    
    short presidentsCode = encryptTheLaunchCodes(randNum);
    gdubya likes to keep it simple.
    Last edited by 7stud; 03-08-2006 at 10:09 PM.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    smartass
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    7stud does have a point though. One could go and do something like the HotBits setup.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I've seen people discuss using static (as in using a radio tuned to nothing) to generate randomness...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    just an idea...

    can i use an unallocated memory, or a random memory address, cast what3v3r values it has as an integer and use it as a random number. provided that it will not change it in anyway just read and maybe copy to a variable?

    crazy idea... (please don't flame me...)

    TIA.
    Last edited by what3v3r; 03-09-2006 at 03:42 AM.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > can i use an unallocated memory, or a random memory address
    No.
    Even just reading random addresses which you don't own can cause things like segmentation faults.

  15. #15
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Quote Originally Posted by what3v3r
    just an idea...

    can i use an unallocated memory, or a random memory address, cast what3v3r values it has as an integer and use it as a random number. provided that it will not change it in anyway just read and maybe copy to a variable?

    crazy idea... (please don't flame me...)

    TIA.

    Memory contents are very poor sources of randomness. In particular, most OSs will zero memory before providing it to a process, which makes the memory non-random in the first place. Second, it is a very rare data set that randomly distributed data and binaries are far from random. However, if you want to run a hash algorithm (like SHA-256) on a chunk of memory then you might be able to use that as the initiation to a pseudo random number generator. You would have to build the functionality into the Kernel as only the Kernel has full access to memory.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM