New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Random Numbers in C


Post Reply New Thread Subscribe

 
Thread Tools
Babkockdood

 
Member Since: Oct 25, 2009
Posts: 59
Babkockdood is on a distinguished road

Babkockdood is offline
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?
QUOTE Thanks
Babkockdood

 
Member Since: Oct 25, 2009
Posts: 59
Babkockdood is on a distinguished road

Babkockdood is offline
Nevermind.
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09

nabl is offline
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.
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,600
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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
QUOTE Thanks
nabl

 
nabl's Avatar
 
Member Since: Dec 13, 2007
Location: United States of America
Posts: 256
nabl has a spectacular aura about
Mac Specs: 2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork 09

nabl is offline
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« Searching for an OSX developer | Shoutcast installation in linux »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Phone numbers disappear in Address Book Juba22 OS X - Apps and Games 0 12-14-2009 08:48 AM
iTunes Track numbers with audible files charliexedd OS X - Apps and Games 1 11-07-2009 02:28 PM
Copying data from email into numbers on iMac Andy and Shell Apple Desktops 0 03-08-2009 05:22 PM
Cutting and Pasting from emails into Numbers Andy and Shell Switcher Hangout 0 03-08-2009 05:20 PM
console thread from a random restart in Leopard webbyhere@gmail OS X - Operating System 0 06-13-2008 11:31 AM

All times are GMT -4. The time now is 02:26 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?