Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Random Numbers in C
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Babkockdood" data-source="post: 1037732" data-attributes="member: 128873"><p>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.</p><p></p><p>[CODE]#include <stdio.h></p><p>#include <stdlib.h></p><p>#include <time.h></p><p></p><p>int main()</p><p>{</p><p> </p><p> srand(time(NULL));</p><p> int number = rand() % 3;</p><p> </p><p> if (number = 1)</p><p> printf("This is the first line of text.\n");</p><p> </p><p> if (number = 2)</p><p> printf("This is the second line of text.\n");</p><p>}[/CODE]</p><p></p><p>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?</p></blockquote><p></p>
[QUOTE="Babkockdood, post: 1037732, member: 128873"] 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"); }[/CODE] 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]
Verification
Name this item 🌈
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Random Numbers in C
Top