Srand and Rand not working correctly?

This is a discussion on Srand and Rand not working correctly? within the C Programming forums, part of the General Programming Boards category; Hi! For some reason my srand and rand functions in my program are returning different numbers, but only thousandths to ...

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

    Srand and Rand not working correctly?

    Hi!

    For some reason my srand and rand functions in my program are returning different numbers, but only thousandths to ten thousandths apart, so not very random. A few days before however, the program was running fine. I even reinstalled my compiler, CodeBlocks.
    Here is an example of a program that has this issue.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>

    Code:
    int main(void)
    {
    srand(time(NULL));
    
    double z;
    
      z = (2.0 * rand()/RAND_MAX - 1.0) *;
    
      printf("%f", z);
    
      return 0;
    
    
      }
    this returns me values such as -0.79283 and then -0.79285

    Please help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what's the problem?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    rand() is basically giving me values that are all extremely close to eachother

    Ah nevermind i think ive fixed the problem, sorry

    nevermind again..... for some reason srand and rand are just not working for me!!! rand() is returning values that are only a thousandth or so in difference each time
    Last edited by lebronlin; 04-05-2011 at 05:27 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      z = (2.0 * rand()/RAND_MAX - 1.0) *;
    Why is that there?

    Anyway, there's nothing that says if I flip a coin that it has to alternate between values. I can flip heads 100 times in a row if I get lucky enough.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    sorry that wasnt supposed to be there but shouldn't the usage of srand(time(NULL)) cause the rand() function to deviate a bit more each time the program is used?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lebronlin View Post
    Hi!

    For some reason my srand and rand functions in my program are returning different numbers, but only thousandths to ten thousandths apart, so not very random. A few days before however, the program was running fine. I even reinstalled my compiler, CodeBlocks.
    Here is an example of a program that has this issue.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>

    Code:
    int main(void)
    {
    srand(time(NULL));
    
    double z;
    
      z = (2.0 * rand()/RAND_MAX - 1.0) *;
    
      printf("%f", z);
    
      return 0;
    
    
      }
    this returns me values such as -0.79283 and then -0.79285

    Please help!
    what is rand()/RAND_MAX ... think about this, RAND_MAX is always higher than the return from rand()... that's what it describes the maximum return. Thus the returned value is always going to be less than 1 and greater than 0...

    If you're looking for a little more range try
    Code:
    z = (double) rand() % MyMax;
    Where MyMax = the highest number you want.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    Thanks for the recommendation, but that's not the issue that I'm trying to solve (sorry if I sound a bit mean)

    Here's a simpler piece of code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int main()
    {
        double x;
        srand(time(NULL));
        x = rand();
        printf("%f", x);
        return 0;
    }
    this piece of code again returns me values such as 14267.00000, 14270.00000, 14273.00000 and just went on as I ran the program more.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by lebronlin View Post
    Thanks for the recommendation, but that's not the issue that I'm trying to solve (sorry if I sound a bit mean)

    Here's a simpler piece of code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int main()
    {
        double x;
        srand(time(NULL));
        x = rand();
        printf("%f", x);
        return 0;
    }
    This seems to be due to a really stupid algorithm used for srand(). Probably it just sets the initial random state to the value you pass to srand() -- since that value increases slowly, the initial random value also increases slowly along with it.

    Try looping multiple times and print out the values you get:

    Code:
    int main()
    {
        double x;
        int i;
        srand(time(NULL));
    
        for (i = 0; i < 50; ++i)
        {
            x = rand();
            printf("%f", x);
        }
        return 0;
    }
    I think you'll see that it's just the first value which is bogus. Either way, it seems the rand() implementation that came with your compiler is pretty crappy.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So instead of running the same thing over and over, call rand in a loop.
    Code:
    for( x = 0; x < 1000; x++ )
        printf( "%08d  %c", rand(), x % 9 == 0 ? '\n' : ' ' );
    edit - brew beat me to it

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    18
    Yeah, I was just wondering if there was a reason why the first value was always bogus , I guess ill be getting rid of CodeBlocks

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by lebronlin View Post
    Yeah, I was just wondering if there was a reason why the first value was always bogus , I guess ill be getting rid of CodeBlocks
    You might be able to work around it by calling rand() once to "prime the pump" and skip past that bogus first value. You could also look at a different random number generator like Mersenne Twister...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Apart from the fact that you shouldn't print an int using %f.
    [cue sound of Doh!!]

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonoob View Post
    Apart from the fact that you shouldn't print an int using %f.
    x is a double, assuming you are talking to brewbuck.
    Quote Originally Posted by nonoob View Post
    [cue sound of Doh!!]



    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by lebronlin View Post
    Yeah, I was just wondering if there was a reason why the first value was always bogus , I guess ill be getting rid of CodeBlocks
    Why would you trash a perfectly good IDE when the issue you having is related to the libraries for the compiler?
    What compiler btw?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

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