Like Tree1Likes
  • 1 Post By CommonTater

Problem with compilation "randomness"

This is a discussion on Problem with compilation "randomness" within the C Programming forums, part of the General Programming Boards category; Hi, I have a unique problem and I am not one to post, I read a lot of posts here ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Problem with compilation "randomness"

    Hi,

    I have a unique problem and I am not one to post, I read a lot of posts here as I am still new.

    I have a random function that is basically taking the square root of random numbers. Everything works great! Except that every time I try to run the function again it just uses the same value as before! Even when I try to re-compile. I think the compiler is cheating! Is there any way to get around this?

    So lets say this is my first result:
    a = 4, b=4 so sqrt(16)=4;
    But a and b are randomly generated. However every time I run the code I still get 4 and 4 for a and b.

    Thank you.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Add ...
    Code:
    #include <time.h>
    
    srand(time(NULL));
    ... to the start of your code.

    If you don't seed the random number generator, it will give you the same sequence every time.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Thanks for your response!

    I tried that, however now all my numbers arent random anymore, plus they are out of my range. Is the srand() different than rand() in terms of how it gets the random number? Or am I screwing up something really simple?

    Thank you again!

    EDIT: Nevermind that worked flawlessly! Thank you so much!!!! I have been stuck on that for hours .
    Last edited by Mark054; 11-08-2011 at 10:00 PM. Reason: edit

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Like this...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
      {  int x, y;
    
         srand(time(NULL));
    
         // 10 random numbers between 1 and 100
         for (int x = 0; x < 10; x++)
            { y = (rand() % 100) + 1;
               printf("%d  ",y); }
    
        return 0;
    }
    Look up srand() and rand() in your C Library Documentation ...
    dennis.cpp likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-17-2009, 09:43 AM
  2. Replies: 2
    Last Post: 03-13-2008, 11:34 AM
  3. "Different levels of indirection" compilation error
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-22-2006, 04:01 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

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