Thread: random generators new ways and old ways

  1. #1
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101

    Question random generators new ways and old ways

    Hi
    I have searched and found how you do it with rand function and mod
    IDE also suggests _FRND
    there is also mersienne twister and others
    whats the best for a quiz?
    not want end up with a repeative random generator or exactly the same quiz

    M

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    There are, at least, 2 dozens of "good" PRNGs out there (see here). There is no PRNGs truly random (the P stands for PSEUDO), but the quality of the randomness is pretty close to "random" as one's can get.

    And lack of repetition is not a criteria for the "best" quality of randomness. Take a coin toss as an example: You have only 2 possible values and they will repeat a lot, it doesn't mean the set isn't random... Same thing as D6 dice toss...

    If you want a "real' random generator for computers you'll need an external source of entropy (and luckly, Intel and AMD processors has an "internal" external source - hehe -- using RDRAND instruction or _rdrand??_step() function you'll get random BITS sequences).

    My preference, due to simplicity and speed are, for PRNGs, LCG and xorshift128+.

    If you don't care about speed and want to get a "truly" random values, in Linux/BSD/OSX you can do:

    Code:
    #include <fcntl.h>
    #include <unistd.h>
    
    // Assume /dev/urandom is always readable and we have enough
    // file descriptors left... Should test this claims before using
    // the routine.
    unsigned int getUint32Random( void )
    {
      unsigned int v;
      int fd;
    
      // /dev/random and /dev/urandom, nowadays, uses rdrand on
      // Intel and AMD platforms... It's not garantee it is truly
      // random in all Unixes, but the kernel tries to garantee a
      // flow of random bits.
      //
      // You can use /dev/random, but this other device will
      // try to garantee entropy (and could block from time to time).
      fd = open( "/dev/urandom", O_RDONLY );
      read( fd, &v, sizeof v );
      close( fd );
    
      return v;
    }
    Last edited by flp1969; 04-20-2019 at 07:09 AM.

  3. #3
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    thanks flp1969

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by flp1969
    It's not garantee it is truly random in all Unixes, but the kernel tries to garantee a flow of random bits.
    I'd say /dev/urandom is guaranteed not to be "truly random" by design parameters, but that generally doesn't matter since a cryptographically secure PRNG seeded with sufficient entropy should be practically indistinguishable from a "truly random" source, whether it is used as a fallback or as an integral part of the system.
    Last edited by laserlight; 04-20-2019 at 02:19 PM.
    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

  5. #5
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    thanks Laserlight

    it seems to work good now produce random facts

  6. #6
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Next step: random generator that generates same seed= same world?
    you tell me you can C,why dont you C your own bugs?

  7. #7
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Code:
    //maybe this work
    /////////////////////////////////////////////
    //srand and rand in a feedback loop,starting with srand(1) or start with a big number
    while (finished==true){
    R=Rand();
    srand(R);
    x=R%numberofrandoms;
    drawsomething(x);
    if screenfilled()==1 {
    finished=True;
    }
    }
    you tell me you can C,why dont you C your own bugs?

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    This will create the SAME sequence every time... it is NOT random or "psedo" random.

    Quote Originally Posted by I C everything View Post
    Code:
    //maybe this work
    /////////////////////////////////////////////
    //srand and rand in a feedback loop,starting with srand(1) or start with a big number
    while (finished==true){
    R=Rand();
    srand(R);
    x=R%numberofrandoms;
    drawsomething(x);
    if screenfilled()==1 {
    finished=True;
    }
    }

  9. #9
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    this purpose is to generate lots of trees for a forest,buildings and such on a map until its filled,so you always endup with same
    you tell me you can C,why dont you C your own bugs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. better ways to use a for loop:
    By jocdrew21 in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2014, 03:00 PM
  2. different ways of typedefing
    By dayalsoap in forum C Programming
    Replies: 8
    Last Post: 09-14-2010, 05:35 AM
  3. different ways to program
    By herWter in forum Windows Programming
    Replies: 3
    Last Post: 07-07-2008, 12:41 AM
  4. Different ways to add
    By mike_g in forum C Programming
    Replies: 11
    Last Post: 03-18-2008, 08:32 AM
  5. HELP! im looking for different ways .
    By moenia in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2002, 12:51 PM

Tags for this Thread