Thread: rand()

  1. #1
    Unregistered
    Guest

    rand()

    How do you write two separate functions using rand() to randomly pick a number 1-12 and then write one to randomly pick a character(the five math operators) without being in the main ().

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    PHP Code:
    #include <iostream.h> //cout,cin
    #include <stdlib.h>   //srand(), rand()
    #include <time.h>     //time()

    char Operators[] = {'+','-','*','/','^'};

    char GetRandomOperator()
    {
        return 
    Operators[rand() % 5];
    }

    int GetRandomNumber(long Lowlong High)
    {
        if(
    Low >= High)
            return 
    0;
        return (
    rand() % (High Low 1)) + Low;
    }


    void main()
    {
        
    srand(time(NULL));
        
    cout<<"A random operator: "<<GetRandomOperator()<<"\nA random number between 1 and 12: "<<GetRandomNumber(1,12)<<"\n";    
        
    cin.get();

    Last edited by XSquared; 07-07-2002 at 08:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM