rand() function is not really random

This is a discussion on rand() function is not really random within the C Programming forums, part of the General Programming Boards category; I made a dice program using Code: dice=rand()%6 + 1; It gives random values from 1 to 6 but every ...

  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
    20,954
    Have you used srand to seed the PRNG with a different seed on each run of the program?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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, 12: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, 07:19 AM
  5. Rand() won't be random
    By EvilPickles in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2006, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21