It seg faults

This is a discussion on It seg faults within the C Programming forums, part of the General Programming Boards category; I'm trying to use a random number generator to index through an array. I get the numbers and have printed ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    22

    It seg faults

    I'm trying to use a random number generator to index through an array. I get the numbers and have printed them to the screen but I cant seem to get anything to work past that. I've tried to store the random numbers in a variable, and it just prints the garbage.

    Thanks
    Code:
    	static const char *list[100] = { "the", "of", "and", "a", "to", "in", "is", "you",
    	"that", "it", "he", "was", "for", "on", "are", "as", "with", "his", "they",
    	"I", "at", "be", "this", "have", "from", "or", "one", "had", "by",
    	"word", "but", "not", "what", "all", "were", "we", "when", "your", "can",
    	"said", "there", "use", "an", "each", "which", "she", "do", "how", "their",
    	"if", "will", "up", "other", "about", "out", "many", "then", "them", "these",
    	"so", "some", "her", "would", "make", "like", "him", "into", "time", "has",
    	"look", "two", "more", "write", "go", "see", "number", "no", "way", "could",
    	"people", "my", "than", "first", "water", "been", "call", "who", "oil",
    	"its", "now", "find", "long", "down", "day", "did", "get", "come", "made",
    	"may", "part"};
    .
    .
    .
     		/*sets the bounds*/
    		printf("%d\n", rand() % (HIGH - LOW + 1) +LOW );
    
    		x = rand();
    		printf("%d\n", x);
    
    				
    		/* print word from array*/
    		printf("%s\n", list[rand()] );

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    printf("%s\n", list[rand()] );
    The odds of a brand-new random number that you pick here being within the bounds that you set are small. Why not use
    Code:
    printf("%d\n", x=rand() % (HIGH - LOW + 1) + LOW);
    printf("%d\n", x);
    printf("%s\n", list[x]);
    Remember: every different call to rand() gets a different number.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    22
    That did it thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2009, 10:29 PM
  2. Seg faults. structs. pointers.
    By ominub in forum C Programming
    Replies: 12
    Last Post: 05-03-2009, 08:04 PM
  3. Help with arrays and seg faults
    By IdioticCreation in forum C++ Programming
    Replies: 7
    Last Post: 04-03-2008, 04:16 PM
  4. sorted linked list...why seg faults!
    By S15_88 in forum C Programming
    Replies: 4
    Last Post: 03-24-2008, 12:30 PM
  5. prog runs on 64bit - seg faults on 32bit
    By hollie in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:59 AM

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