C++ Random Number - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 1, 2007, 03:12 PM   #1
ghall
macrumors 68040
 
ghall's Avatar
 
Join Date: Jun 2006
Location: Rhode Island
Send a message via AIM to ghall
C++ Random Number

I'm writing a program that requires a random action, and I've found a way to do it, which is assign a random number from 0 to 9 to 'x', and then create some 'if' and 'if else' commands using the value of 'x' as a guard.

I looked up 'rand', and I really don't understand how to use it.
ghall is offline   0 Reply With Quote
Old Nov 1, 2007, 03:55 PM   #2
ukneeq
macrumors member
 
Join Date: Jul 2007
Location: Lewisville, TX
Send a message via AIM to ukneeq
Quote:
Originally Posted by ghall View Post
I'm writing a program that requires a random action, and I've found a way to do it, which is assign a random number from 0 to 9 to 'x', and then create some 'if' and 'if else' commands using the value of 'x' as a guard.

I looked up 'rand', and I really don't understand how to use it.
Here is a link to a good reference:
http://www.cplusplus.com/reference/c...dlib/rand.html

but basically you just set the variable to rand() % 10. The number you use the modulo with is the values you want to get a random number from. So in your case you would just have

x = rand() % 10;
ukneeq is offline   0 Reply With Quote
Old Nov 1, 2007, 04:26 PM   #3
ghall
Thread Starter
macrumors 68040
 
ghall's Avatar
 
Join Date: Jun 2006
Location: Rhode Island
Send a message via AIM to ghall
Quote:
Originally Posted by ukneeq View Post
Here is a link to a good reference:
http://www.cplusplus.com/reference/c...dlib/rand.html

but basically you just set the variable to rand() % 10. The number you use the modulo with is the values you want to get a random number from. So in your case you would just have

x = rand() % 10;
Yay, thanks!
ghall is offline   0 Reply With Quote
Old Nov 1, 2007, 04:44 PM   #4
ghall
Thread Starter
macrumors 68040
 
ghall's Avatar
 
Join Date: Jun 2006
Location: Rhode Island
Send a message via AIM to ghall
Okay new problem. It keeps returning the same number.

Here's my code:
Code:
#include <stdio.h>	
#include <stdlib.h>
int main()


{
	int libchoose = rand() % 10;
	
	
	if (libchoose==0){
		char verbpt[50];
		char adj[50];
		char verb[50];
		char noun[50];
		char verb2[50];
	
		printf ("Verb, Present Tense: ");
		scanf ("%s", verbpt);
		printf ("Adjective: ");
		scanf ("%s", adj);
		printf ("Verb: ");
		scanf ("%s", verb);
		printf ("Noun: ");
		scanf ("%s", noun);
		printf ("Verb: ");
		scanf ("%s", verb2);
	
		//"Flying is easy. You just have to throw yourself at the ground and miss"-Douglas Adams
		printf ("%s is %s. You just %s yourself at the %s and %s.", verbpt, adj, verb, noun, verb2);
	}
	
	else if (libchoose==1){
		
		//"I must say[verb] I find television[noun] very educational[adj]. The minute somebody turns it on, I go to the library[noun2] and read a good[adj2] book[noun3]."-Groucho Marx
		char noun[50];
		char adj[50];
		char noun2[50];
		char adj2[50];
		char noun3[50];
		

		printf ("Noun: ");
		scanf ("%s", noun);
		printf ("Adjective: ");
		scanf ("%s", adj);
		printf ("Noun: ");
		scanf ("%s", noun2);
		printf ("Adjective: ");
		scanf ("%s", adj2);
		printf ("Noun: ");
		scanf ("%s", noun3);
		
		printf ("I must say I find %s very %s. The minute somebody turns it on, I go to the %s and read a %s %s", noun, adj, noun2, adj2, noun3);
		
	
	}
	else{
		printf ("Puzzle %d is not a valid puzzle.\n", libchoose);
	}
	
}

Last edited by ghall; Nov 1, 2007 at 04:51 PM.
ghall is offline   0 Reply With Quote
Old Nov 1, 2007, 05:18 PM   #5
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
random() is a better algorithm than rand() its more random, you seed it with srandomdev()
Eraserhead is offline   0 Reply With Quote
Old Nov 1, 2007, 05:30 PM   #6
ghall
Thread Starter
macrumors 68040
 
ghall's Avatar
 
Join Date: Jun 2006
Location: Rhode Island
Send a message via AIM to ghall
Quote:
Originally Posted by Eraserhead View Post
random() is a better algorithm than rand() its more random, you seed it with srandomdev()
What does seeding mean?
ghall is offline   0 Reply With Quote
Old Nov 1, 2007, 06:01 PM   #7
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by ghall View Post
What does seeding mean?
It seeds the random number with the date, so you don't get the same sequence of random numbers each time .
Eraserhead is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Your Apple store order number is also your UPS reference number Gjwilly iPhone 0 Sep 23, 2013 09:36 AM
4S- Changed phone number, old number still shows up in messages dbabytro iPhone 4 Jul 18, 2013 01:58 PM
Good, Portable Random Number Generator for C++? ArtOfWarfare Mac Programming 19 Jan 10, 2013 05:59 PM
MBP 2010 random/not so random crashes? Kobaine MacBook Pro 0 Jan 3, 2013 04:10 PM
Creating a random number within a URL using Applescript moorbull Mac Programming 1 Jul 19, 2012 08:16 AM

Forum Jump

All times are GMT -5. The time now is 12:09 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC