C++ Random Number

Discussion in 'Mac Programming' started by ghall, Nov 1, 2007.

  1. macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #1
    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.
     
  2. macrumors member

    Joined:
    Jul 13, 2007
    Location:
    Lewisville, TX
    #2
    Here is a link to a good reference:
    http://www.cplusplus.com/reference/clibrary/cstdlib/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;
     
  3. thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #3
    Yay, thanks!
     
  4. thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #4
    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);
    	}
    	
    }
    	
    
    
    
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    random() is a better algorithm than rand() its more random, you seed it with srandomdev()
     
  6. thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #6
    What does seeding mean?
     
  7. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    It seeds the random number with the date, so you don't get the same sequence of random numbers each time ;).
     

Share This Page