Thread: rand() function is not really random

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    rand() function is not really random

    I made a dice program using
    Code:
    dice=rand()%6 + 1;
    It gives random values from 1 to 6 but every time I run the program, it just gives me the same set of random values.I tried using
    Code:
    random=(rand()*time(NULL))%6 + 1;if(random>0) //since random sometimes become negative{     dice=random; }
    But this one always shows 1 and sometimes the other numbers. It's like 90% chance of getting 1 and I do not know why.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you used srand to seed the PRNG with a different seed on each run of the program?
    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

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I think you forgot to seed srand() with a new value each time(at the beginning).
    srand(time(NULL)) is often used, as everytime you run, the time is going to differ slightly.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    [FONT=Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace]seed it...

    use srand function..

    Code:
    srand(time(NULL));
    [/FONT]

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You could do any of these options as well:

    - Use a more cryptographically secure random generator. Arc4random() is not standard, but it's available on pretty much every Linux/Mac/*nix build I've seen. Also, a whole array of functions like this can be found here > jrand48(3) - Linux man page

    - Seed your random, using srand(time(NULL));

    - Write a "hack" quick random function:
    Code:
    #include <sys/time.h>
    
    
    uint64_t fake_rand(int clamp)
    {
    	uint64_t val;
    
    
    	asm ("rdtsc" : "=A" (val) : : );
    
    
    	return (val % clamp);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random rand() question
    By Nathan the noob in forum C++ Programming
    Replies: 13
    Last Post: 01-27-2009, 01:52 PM
  2. rand() function not acting very random
    By yougene in forum C Programming
    Replies: 5
    Last Post: 08-28-2007, 11:02 PM
  3. Rand() not bieng very random
    By Necrofear in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2007, 08:12 PM
  4. rand() not so random....
    By Kewley in forum C++ Programming
    Replies: 11
    Last Post: 01-23-2007, 08:19 AM
  5. Rand() won't be random
    By EvilPickles in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2006, 01:47 AM