Results 1 to 5 of 5
  1. #1


    Member Since
    Oct 25, 2009
    Posts
    59
    Random Numbers in C
    OK, I have been trying for about an hour now to get this to work. I'm trying to generate a random number and display a line of text based on that number. This is what I have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    	
    	srand(time(NULL));
    	int number = rand() % 3;
    	
    	if (number = 1)
    		printf("This is the first line of text.\n");
    	
    	if (number = 2)
    		printf("This is the second line of text.\n");
    }
    I want this to print either "This is the first line of text." or "This is the second line of text.". When I compile and run it, it prints both. Can someone help me with this?

  2. #2


    Member Since
    Oct 25, 2009
    Posts
    59
    Nevermind.

  3. #3

    nabl's Avatar
    Member Since
    Dec 13, 2007
    Location
    United States of America
    Posts
    256
    Specs:
    2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09
    I assume you figured it out, but just in case you didn't, the problem is that you're using the assignment operator (=) in your comparisons instead of the correct == operator. Also, considering that you want to print either one line of text or the other, you might want to consider using an if/else construction.

  4. #4

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,548
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 6S, iPod Nano 7th GeniPad 3
    Also, you aren't accounting for the very real possibility that number = 0. If you only want to check for 1 and 2, then you need "(rand() &#37; 3) + 1)" to cover that..

    Regards

  5. #5

    nabl's Avatar
    Member Since
    Dec 13, 2007
    Location
    United States of America
    Posts
    256
    Specs:
    2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09
    Quote Originally Posted by Raz0rEdge View Post
    Also, you aren't accounting for the very real possibility that number = 0. If you only want to check for 1 and 2, then you need "(rand() % 3) + 1)" to cover that..

    Regards
    Or more simply, if only two options are wanted, int number = rand() % 2; followed by a check for 0 or 1 would be best.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Microsoft Excel: How to create random numbers in cells?
    By simonvee in forum OS X - Apps and Games
    Replies: 3
    Last Post: 08-11-2015, 01:00 PM
  2. Replies: 0
    Last Post: 08-23-2011, 04:14 AM
  3. Numbers Help
    By CPM in forum OS X - Apps and Games
    Replies: 3
    Last Post: 05-08-2011, 12:34 PM
  4. Numbers
    By jasonrlee in forum OS X - Apps and Games
    Replies: 1
    Last Post: 03-31-2008, 09:05 AM
  5. Correlating Model Numbers and Apple Part Numbers
    By fziksman in forum Other Hardware and Peripherals
    Replies: 0
    Last Post: 01-30-2007, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •