HELP with srand(time()( in Code::Blocks (C language)

This is a discussion on HELP with srand(time()( in Code::Blocks (C language) within the C Programming forums, part of the General Programming Boards category; Does anyone one know why srand(time()) is not working in Code::Blocks (C programming)? Below the simple codes I wrote. The ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    HELP with srand(time()( in Code::Blocks (C language)

    Does anyone one know why srand(time()) is not working in Code::Blocks (C programming)?

    Below the simple codes I wrote. The result is always the same: "Program has stopped working". THANK YOU!

    insert
    Code:
     
    
    #include<stdio.h>
    main()
    {
        int iSelection, x, iRandom1, iRandom2, iResult;
        srand(time());
    
        printf("\n\tEnter here the number of questions you want: ");
        scanf("%d", &iSelection);
    
        for(x = 0; x < iSelection; x++)
        {
            printf("\nWhat is the result of %d x %d? ", iRandom1, iRandom2);
            scanf("%d", &iResult);
    
            if(iResult == iRandom1 * iRandom2)
            {
                printf("\nThat is correct!\n");
            }
            else
            {
                printf("\nThat is not correct; the right answer is %d\n", iRandom1 * iRandom2);
            }
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well if you included time.h, you would have a proper prototype for time(), and an error message.

    Also, your two rand variables are uninitialised.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,193
    You won't know if "srand()" works until you actually call the random number function.

    Hint #1: This is related to Salem's second point.
    Hint #2: Cprogramming.com FAQ > Generate random numbers?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Thanks a lot Salem

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Thanks a lot Matticus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-23-2010, 05:09 AM
  2. srand(time()) returns segmentation fault
    By withoutn in forum C Programming
    Replies: 9
    Last Post: 07-03-2007, 01:59 PM
  3. code::blocks
    By wart101 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-18-2007, 06:42 AM
  4. code::blocks
    By wart101 in forum Tech Board
    Replies: 4
    Last Post: 01-15-2007, 06:38 AM
  5. srand(time(NULL));
    By vin0_oce in forum C Programming
    Replies: 4
    Last Post: 09-28-2002, 08:34 PM

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