6 Replies - 2064 Views - Last Post: 16 April 2010 - 01:07 AM Rate Topic: -----

#1 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Trying to figure out random number generator for magic 8-ball type pro

Posted 15 April 2010 - 01:40 AM

Hello I'm a bit new to the coding scene, but i already know most of the syntax used in C++ programming as I've read through the syntax portion of C++ for dummies countless times. one thing i am having trouble with is the random number generator function. Here is my code i will describe my problem after the break.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;


int main()
{
    
    srand((unsigned)time(0));
    int random_integer = rand()%1000;
    cout << random_integer << endl;     
    
    system("PAUSE");
    return 0;
    
}
 



what my problem is is when i run the program every time its just counting up. and depending on the speed at which i run it it might go up 3 or 7. or even more if i wait longer before i run it again. the program i wish to use this generator in will be of the magic 8-ball variety. and i will be using cases to output the different messages.
i would like to be able to run it once and have a number like say 653 then run it a few seconds later. or even in the same instance with a new question for the 8-ball. and then come up with a number like 294 and a few seconds later say 999. and so on and so forth. if you could help me out with this it would be greatly appreciated. sorry about punctuation and capitalization, but my computers brand new and im still not used to the funky keyboard for typing fast yet.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to figure out random number generator for magic 8-ball type pro

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Trying to figure out random number generator for magic 8-ball type pro

Posted 15 April 2010 - 02:14 AM

From looking at your code, I cannot see any issues. You should get a random number between 0 and 999, but if you say you are not, I would suggest you try the following:

#include <cstdio> 
#include <cstdlib> 
#include <iostream> 
#include <ctime> 
using namespace std; 
 
 
int main() 
{ 
     
    srand((unsigned)time(0)); 
    int random_loop_integer = rand()%1000;
    for ( ; random_loop_integer > 0; --random_loop_integer) rand()%1000;
    int random_integer = rand()%1000;
    cout << random_integer << endl;      
     
    system("PAUSE"); 
    return 0; 
     
} 



I have not tested the code above, but basically I am generating a random number, then using that to get the Nth random number for output. See where I am going with this?
Was This Post Helpful? 0
  • +
  • -

#3 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Trying to figure out random number generator for magic 8-ball type pro

Posted 15 April 2010 - 02:18 AM

okay from how i understand this you are generating a random number off the time then using that to seed another random number. and again . and then once more for good measures. thank you very much i will try out the code and see what happens. if it works ill upload the finished project to the snippets section within about 2 hours.
Was This Post Helpful? 0
  • +
  • -

#4 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Trying to figure out random number generator for magic 8-ball type pro

Posted 15 April 2010 - 02:29 AM

Okay sorry about the double forgot where edit was. one thing i dont understand with this project is i would like to use switch statements for controlling the program. one with " press 1 to ask the magic 8-ball a question" and another with " press 2 to exit"

after that i would like to use the number that the above random number generator outputs to controll another switch statement with things such as yes and no as the outputs. what im not sure about is how to implement the switch command like this... or really at all for that matter.

i would also like to do it such that after one question is asked it will loop back up the the "1 for question 2 for exit" prompt. i understand i could probably use a do while loop for the first one and just have it do while(confirm = 1) and then an else break. not entirely sure on the structure of this kind of loop with the switch command in it at all. thanks again for any help.
Was This Post Helpful? 0
  • +
  • -

#5 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Trying to figure out random number generator for magic 8-ball type pro

Posted 15 April 2010 - 11:07 AM

now im having another problem with my program. so far thanks for the random number generator. I've put it inside an if statement in order to decide whether or not the program even runs. the following code should work as follows.
1. prompt for user input (1 to continue 2 to exit)
2. if the user pressed 1 then output the random number. and then pause.
3. if the user pressed 2 then completely exit the program.
heres my code.
#include <cstdio>  
#include <cstdlib>  
#include <iostream>  
#include <ctime>  
using namespace std;  
  
  
int main()  
{  
    //determines whether user would like to get an answer from 8-ball.
      int runtime;
      cout << "press 1 to ask the magic 8-ball a question, or press another number to exit\n";
      cin >> runtime;
      if (runtime = 1) {
                
           srand((unsigned)time(0));  
           int random_loop_integer = rand()%1000; 
           for ( ; random_loop_integer > 0; --random_loop_integer) rand()%1000; 
           int random_integer = rand()%1000; 
           cout << random_integer << endl;
      }
      else {
           if (runtime = 2) {
                       return 0;
                       }
           }
      
      
             
      
    system("PAUSE");  
    return 0;  
      
} 



I realize this may be easier done with a switch statement, but im not really sure how to implement this.
Was This Post Helpful? 0
  • +
  • -

#6 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Trying to figure out random number generator for magic 8-ball type pro

Posted 15 April 2010 - 11:15 AM

myvar = 1;
while(myvar == 1)
{
    //Run the magic 8 ball program


   //Tell user to enter 1 to ask another question, or 2 to exit
   //Store their input in myvar, it will either exit or loop around again.


}



As far as using the random number to get a random answer. I am sure there are many ways to accomplish this. One suggestion is to use simple if statements. Remember, switch statements cannot use expressions, only numbers, so you can't do any type of calculation in a switch, which means you would have to write a switch case for every single possible random number. You might try something like the following.

if(the_random_number < 200)
{
    //give them an answer
}
else if(the_random_number > 199 && the_random_number < 400)
{
    //give them a different answer
}
//and so on



Hope that helps.

int main()  
{  
    //determines whether user would like to get an answer from 8-ball.
      int runtime;
      cout << "press 1 to ask the magic 8-ball a question, or press another number to exit\n";
      cin >> runtime;
      if (runtime = 1) { //You are assigning runtime to equal 1 in this line
                
           srand((unsigned)time(0));  
           int random_loop_integer = rand()%1000; 
           for ( ; random_loop_integer > 0; --random_loop_integer) rand()%1000; //please review this line of code
           int random_integer = rand()%1000; 
           cout << random_integer << endl;
      }
      else {
           if (runtime = 2) { //You assign runtime to equal 2
                       return 0;
                       }
           }
      
      
             
      
    system("PAUSE");  
    return 0;  
      
} 


Was This Post Helpful? 0
  • +
  • -

#7 codermonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 15-April 10

Re: Trying to figure out random number generator for magic 8-ball type pro

Posted 16 April 2010 - 01:07 AM

okay i got her figured out heres my code below for the finished project.
// Magic 8-ball with multiple answers using cases.
//
//
//
#include <cstdio>  
#include <cstdlib>  
#include <iostream>  
#include <ctime>  
using namespace std;  

  
int main()  
{  
    //determines whether user would like to get an answer from 8-ball.
      int runtime;
      cout << "press 1 to ask the magic 8-ball a question, or press 2 to exit\n";
      cout << "press 3 to view disclaimer and copyright info\n";
      cin >> runtime;
      int random_loop_integer;
      int random_integer;
      string question;
      switch ( runtime ) 
      {
           case 1:
           do {     
           
           srand((unsigned)time(0));  
           random_loop_integer = rand()%1000; 
           for ( ; random_loop_integer > 0; --random_loop_integer) rand()%1000; 
           random_integer = rand()%1000; 
          // cout << random_integer << endl;
           cout << "think about your question deeply for a moment and then press 1 to continue\n";
           cin >> question;
           if (random_integer <50)
           {
              cout << "yes\n";
           }
           else if (random_integer >51 && random_integer < 100)
           {
                cout << "No\n";
           }
           else if (random_integer > 101 && random_integer < 150)
           {
                cout << "maybe\n";
           }
           else if (random_integer > 151 && random_integer < 200)
           {
                cout << "the answer is unclear\n";
           }
           else if (random_integer > 201 && random_integer < 250)
           {
                cout << "definately\n";
           }
           else if (random_integer > 251 && random_integer < 300)
           {
                cout << "definately not\n";
           }
           else if (random_integer > 301 && random_integer < 350)
           {
                cout << "try again later\n";
           }
           else if (random_integer > 351 && random_integer < 400)
           {
                cout << "it would be unwise\n";
           }
           else if (random_integer > 401 && random_integer < 450)
           {
                cout << "I knew yesterday...\n";
           }
           else if (random_integer > 451 && random_integer < 500)
           {
                cout << "Is it really something your parents would approve of\n";
           }
           else if (random_integer > 501 && random_integer < 550)
           {
                cout << "Who's on first?\n";
           }
           else if (random_integer > 551 && random_integer < 600)
           {
                cout << "I think you should ask someone of your age\n";
           }
           else if (random_integer > 601 && random_integer < 650)
           {
                cout << "why ask me all the hard questions?\n";
           }
           else if (random_integer > 651 && random_integer < 700)
           {
                cout << "what do i look like, a computer?\n";
           }
           else if (random_integer > 701 && random_integer < 750)
           {
                cout << "does not compute\n";
           }
           else if (random_integer > 751 && random_integer < 800)
           {
                cout << "should you really be thinking about things like that at your age?\n";
           }
           else if (random_integer > 801 && random_integer < 850)
           {
                cout << "how old did you say you were?\n";
                
           }
           else if (random_integer > 851 && random_integer < 900)
           {
                cout << "WHAT?!?\n";
           }
           else if (random_integer > 901 && random_integer < 950)
           {
                cout << "whats that?\n";
           }
           else if (random_integer > 951 && random_integer < 1000)
           {
                cout << "come back next tuesday\n";
           }
           cout << "press 1 to ask another question. to exit press any other number.\n";
           cin >> runtime;
           
           
           } while (runtime < 2);
           break;
           case 2:
                return 0;
                break;
           case 3:
                cout << " this program has no express or limited warranty and any";
                cout << " damage to your system cannot be held againnst me or any";
                cout << " of the people who helped me code this project\n";
                cout << " copyright david nelson AKA codermonk on dreamincode.net";
                cout << " use of this code for your project is not allowed.";
                cout << " but feel free to use short snippets for your own use.";          
      }  
    system("PAUSE");  
    return 0;
} 


thank you to all who have helped me on this project i will post it in the snippets section if so desired. of the next 3 people to view this thread please vote whether or not i should post this in the snippets section.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1