Thread: Can't figure out what's wrong with my Random Number Generator in C

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    5

    Can't figure out what's wrong with my Random Number Generator in C

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
        #define MAX_VALUE = 6;
        #define MIN_VALUE = 1;
        const int NUMBER_OF_VALUES =(6 - 1 + 1);
        const int NUMBER_OF_TRIALS = 1000;
        
        int main()
        {
            int count[NUMBER_OF_VALUES];
            int i;
            int value = i + MIN_VALUE;
            for (i=0;i<NUMBER_OF_TRIALS;i++)
            {
                int position;
                int random;
                double randomReal;
                randomReal = rand()/(((double)RAND_MAX)+1)) * NUMBER_OF_VALUES + MIN_VALUE;
                random = (int)(randomReal);
                position = random - MIN_VALUE;
                count[position] = count[position]+1;
            }
            printf("Counts\n");
            for (i=0;i<NUMBER_OF_VALUES;i++)
            {
                printf("%d : %d\n",(value), count[i]);
            }
            int expectedCount;
            expectedCount = NUMBER_OF_TRIALS/NUMBER_OF_VALUES;
            printf("The expected count is %d\n",expectedCount);
            printf("Distance From Average\n");
            for (i=0;i<NUMBER_OF_VALUES;i++)
            {
                int distanceFromExpected;
                distanceFromExpected = count[i] - NUMBER_OF_TRIALS/NUMBER_OF_VALUES;
                printf("%d : %d\n",(value),abs(distanceFromExpected));
            }
        }
    Every time I try to run it, I get these errors:

    Code:
    **** Build of configuration Release for project RandomNumberCheckerInC ****
    
    make all 
    Building target: RandomNumberCheckerInC
    Invoking: GCC C Linker
    gcc  -o"RandomNumberCheckerInC"  ./randomNumberChecker.o   
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 10 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 20 has invalid symbol index 20
    /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: ld returned 1 exit status
    make: *** [RandomNumberCheckerInC] Error 1
    Which I figure to mean that there's no main method but I clearly have a main method in my program. I'm just a beginner C
    programmer using Eclipse to build and run my code. Why won't my current code work correctly?
    Last edited by csisock; 09-08-2013 at 08:39 PM.

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    i think you should initialize, intposition, intrandom, doublerandomReal, outside of the for loop.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Eclipse has lines 14, 20, and 22 underlines as 'syntax errors'. All of which include MIN_VALUE so I think the problem lies in there somewhere but I can't figure it out.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    #defines don't end with semi-colons
    #defines and other preprocessor instructions should begin at column 0

    > int i;
    > int value = i + MIN_VALUE;
    i is being used before it is initialized.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    #defines don't end with semi-colons
    #defines and other preprocessor instructions should begin at column 0

    > int i;
    > int value = i + MIN_VALUE;
    i is being used before it is initialized.
    I understand the first part about the #defines, they were just errors on my part because I'm so used to putting semi-colons on everything. But I'm unsure about what you mean with i being used before it's initialized.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    C doesn't give initial values to variables when you define them. You might think i is equal to zero when you define it (and it might turn out that it actually is) but you shouldn't assume that it is. As I said, C doesn't make i zero just because you defined it, you have to set a value yourself.

    It is such a common mistake that it is a warning. And even though you won't say what Eclipse's errors are, that's my best guess.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    C doesn't give initial values to variables when you define them. You might think i is equal to zero when you define it (and it might turn out that it actually is) but you shouldn't assume that it is. As I said, C doesn't make i zero just because you defined it, you have to set a value yourself.

    It is such a common mistake that it is a warning. And even though you won't say what Eclipse's errors are, that's my best guess.
    Alright, so I fixed that but still when I build the code, this is what is output in the console:
    Code:
    **** Build of configuration Release for project RandomNumberCheckerInC ****
     
    make all 
    Building target: RandomNumberCheckerInC
    Invoking: GCC C Linker
    gcc  -o"RandomNumberCheckerInC"  ./randomNumberChecker.o   
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 10 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 20 has invalid symbol index 20
    /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: ld returned 1 exit status
    make: *** [RandomNumberCheckerInC] Error 1
    Please bear with me, I'm used to Java. I'm just trying to learn C on my own because I feel it would be beneficial.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OK, well one thing that can lead to these errors is building incorrectly.

    Code:
    C:\Users\Josh2\Documents>gcc -Wall -std=c99 -c csisock.c
    
    C:\Users\Josh2\Documents>gcc csisock.o -o csisock
    When you want to get an o file, you should use the -c option before you link. When you want to get an exe file, you should use the -o.

    It is also highly likely that the compile stage just never worked, so there was no o file to begin with.
    Code:
    csisock.c: In function 'main':
    csisock.c:14:25: error: expected expression before '=' token
    csisock.c:20:55: error: expected ';' before ')' token
    csisock.c:20:55: error: expected statement before ')' token
    csisock.c:20:57: error: invalid type argument of unary '*' (have 'int')
    csisock.c:20:78: error: expected expression before '=' token
    csisock.c:22:33: error: expected expression before '=' token
    I had to fix these. In addition to the earlier comment I made, you do not need to assign defines. It is a simple text substitution, so all you have to do is write what value you want the define to stand for after some white space. The other errors on line 20 were caused by unbalanced parens.

    If you can't find a place to read Eclipse's error digest and post it here, you really shouldn't be using it. I'm sorry but I just find that unacceptable.

    At any rate I will attach a "fixed" file. Get it to work for you somehow.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using random number generator
    By camel-man in forum C Programming
    Replies: 8
    Last Post: 06-15-2011, 10:19 PM
  2. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  3. random number generator
    By begginer in forum C Programming
    Replies: 1
    Last Post: 02-23-2011, 08:52 AM
  4. random number generator
    By noodle24 in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2006, 10:41 AM
  5. random number generator
    By Verbenaca in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2005, 08:05 AM