Results 1 to 2 of 2

Thread: Random No Generator in C/C++

  1. #1
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Random No Generator in C/C++

    Hi,

    How can we generate a random no without using rand() or any other library function in C/C++.

    Can somebody give some logic......
    Do what u r afraid to do, and the death of fear is sure.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Random No Generator in C/C++

    You should see "numerical recipies in C/C++". They have several excellent methods. If you're not worried about amazingly random numbers try this (a small excerpt from chapter 7):
    #define IA 16807
    #define IM 2147483647
    #define AM (1.0/IM)
    #define IQ 127773
    #define IR 2836
    #define MASK 123459876
    float ran0(long *idum)
    /*“Minimal” random number generator of Park and Miller. Returns a uniform random deviate
    between 0.0 and 1.0. Set or reset idum to any integer value (except the unlikely value MASK)to initialize the sequence; idum must not be altered between calls for successive deviates in a sequence./**/
    {
    long k;
    float ans;
    *idum ^= MASK; XORing with MASK allows use of zero and other
    simple bit patterns for k=(*idum)/IQ; idum.
    *idum=IA*(*idum-k*IQ)-IR*k; Compute idum=(IA*idum) % IM without overif
    (*idum < 0) *idum += IM; flows by Schrage’s method.
    ans=AM*(*idum); Convert idum to a floating result.
    *idum ^= MASK; Unmask before return.
    return ans;
    }

Similar Threads

  1. Replies: 7
    Last Post: 31st May 2006, 10:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.