srand(time()) returns segmentation fault

This is a discussion on srand(time()) returns segmentation fault within the C Programming forums, part of the General Programming Boards category; Hey everyone, Here's another problem I encountered: when I write this: (This is a beginning of a "guess a number ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    srand(time()) returns segmentation fault

    Hey everyone,
    Here's another problem I encountered:

    when I write this: (This is a beginning of a "guess a number game")

    Code:
    #include <stdio.h>
    
    main()
    {
    
      int iRandomNum = 0;
      int iResponse = 0;
      srand(time());
    
      iRandomNum = (rand() % 10) + 1;
    
    ...
    it returns a segmentation fault. I tested multiple instances, and what I observed is, whenever I define a variable as (rand() % 10) + 1 following srand(time()) it doesn't work and I'm not sure why.

    This however works:

    Code:
    #include <stdio.h>
    
    main()
    {
    
      srand(time());
    
      printf("\n%d\n", (rand() % 8) + 1);
    }
    The first code snippet is provided by the book "C programming for the absolute beginner"

    Thanks for all the help

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    time() has to take a parameter. Read the manual! You can pass it NULL:

    Code:
    srand(time(NULL));
    Calling it without the parameter is equal to calling it with garbage. So your program crashes.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Thanks brewbuck for the rapid help.

    It works!

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    wait but actually I have another question, how come it works without a NULL in this code?
    Thanks again for the help,

    Code:
    #include <stdio.h>
    
    main()
    {
    
      int iRandomNum = 0;
      srand(time());
    
      iRandomNum = (rand() % 4) + 1;
    
      printf("\nFortune Cookie-Chapter 3\n");
    
      switch (iRandomNum) {
    
      case 1:
        printf("\nYou will meet a new friend today.\n");
        break;
      case 2:
        printf("\nYou will enjoy a long and happy life.\n");
        break;
      case 3:
        printf("\nOpportunity knocks softly. Can you hear it?\n");
        break;
      case 4:
        printf("\nYou'll be financially rewarded for your good deeds.\n");
        break;
      } // end switch
    } // end main

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by withoutn View Post
    wait but actually I have another question, how come it works without a NULL in this code?
    Random freaking chance. When you play Russian Roulette, 5 times out of 6 you survive.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Hey, it's me again. Sorry for bugging you with all these questions, but I just wrote this, and I get a segmentation fault again.

    Code:
    #include <stdio.h>
    
    main()
    {
    
      int intRandomNum = 0;
      int intResponse = 0;
      srand(time(NULL));
    
      intRandomNum = (rand() % 10) + 1;
    
      printf("\n Guess a number between 1 and 10: ");
      scanf("%d", intResponse);
    
      if (intRandomNum == intResponse)
        printf("\n You got it!\n\n");
      else
        printf("\n Wrong! The correct number is: %d\n\n", intRandomNum);
    }

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Not using scanf() correctly. You need to pass it a pointer to a location to store the result, not the variable itself. Change is made in red below:

    Code:
    #include <stdio.h>
    
    main()
    {
    
      int intRandomNum = 0;
      int intResponse = 0;
      srand(time(NULL));
    
      intRandomNum = (rand() % 10) + 1;
    
      printf("\n Guess a number between 1 and 10: ");
      scanf("%d", &intResponse);
    
      if (intRandomNum == intResponse)
        printf("\n You got it!\n\n");
      else
        printf("\n Wrong! The correct number is: %d\n\n", intRandomNum);
    }

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    Oh yes, now it's fine. Thanks for aaaall the help. Sorry for taking up your time :-)

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it works because you don't include time.h. If you did, it would give you an error.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Are you getting any warnings with this code?
    Are you paying attention to them, or just running the code no matter what?

    Since you seem to be using gcc on Linux, then I would suggest
    gcc -Werror -W -Wall -ansi -pedantic -O2 prog.c
    which would have caught every single problem you've posted so far before you tried to run anything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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