Random Numbers

This is a discussion on Random Numbers within the C Programming forums, part of the General Programming Boards category; Hey, guys. I'm fairly new to the C programming language. I'm trying to generate a random number and display a ...

  1. #1
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352

    Random Numbers

    Hey, guys. I'm fairly new to the C programming language. 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? Thanks.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    use == instead of =
    Devoted my life to programming...

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Sipher View Post
    use == instead of =
    Thank you! Thank you SO MUCH!!!

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Don't mention it. I always want to help others if i can
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM

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