Thread: Help with random in my code

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    11

    Help with random in my code

    I cannot figure out where to put in randomize(); in this code so that the numbers are truly random. I can only get them to stay the same for a long time and then change by 1, or change by 1 each time, or sometimes not change at all. Here is my code...
    Note that fight is a function I call, and that the //1, //2, etc. was just for me to fix some problems with missing brackets.
    Code:
    					
    	if (choice1 == 1) {
    cout<<"He grunts, turns around, and stumbles towards you."<<endl;
    						
    fight (health,stoner_health);}
    
    			}
    	return 0;
    }
    
    fight (int fighter1, int fighter2)
    	{//1
    
    int fight_choice;
    while (fighter2>0 || fighter1>0) {
    cout<<"Do you want to Punch(1) or Kick(2)?"<<endl;
    cin>>fight_choice;
    
    	switch(fight_choice) {//2
    		case 1: {//3
    		int strike;
    		strike = random(5);
    		int damage;
    		damage = random(5);
    		cout<<"You punch for "<< damage << " damage."<<endl;
    		fighter2 = fighter2 - damage;
    		cout<<"They are down to "<< fighter2 << " health"<<endl;
    		cout<<"They attack you for " << strike << " damage."<<endl;
    		fighter1 = fighter1 - strike;
    		cout<<"You are down to " << fighter1 << " health."<<endl;
    		getch();
    		clrscr(); }//3
    		break;
    
    		case 2: {//4
    		int strike2;
    		strike2 = random(8);
    		int damage2;
    		damage2 = random(8);
    		cout<<"You kick for "<< damage2 << " damage, but leave yourself open for attack."<<endl;
    		fighter2 = fighter2 - damage2;
    		cout<<"They are down to "<< fighter2 << " health"<<endl;
    		cout<<"They attack you for " << strike2 << " damage."<<endl;
    		fighter1 = fighter1 - strike2;
    		cout<<"You are down to " << fighter1 << " health."<<endl;
    		getch();
    		clrscr(); 
                                    break; }//4
    			}//2
    			}
    I'm not here. This isn't happening

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    I'm not familiar with randomize, but:
    Code:
    #include <time.h>
    #include <stdio.h>
    
    srand(time(NULL));
    
    someRandomInt=rand() % 100;
    i'm assuming randomize is like the srand call i used, therefore, call it just before you call each random (or in my case rand).
    PHP and XML
    Let's talk about SAX

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Waldo2k2
    I'm not familiar with randomize, but:
    Code:
    #include <time.h>
    #include <stdio.h>
    
    srand(time(NULL));
    
    someRandomInt=rand() % 100;
    i'm assuming randomize is like the srand call i used, therefore, call it just before you call each random (or in my case rand).
    No, call srand(time(NULL)) ONCE at the start of main. After that, use rand().

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    >>once

    hmm, perhaps that's why my random outcomes were so bad in that one program.....lol, thanks eibro
    PHP and XML
    Let's talk about SAX

  5. #5
    this random works best for me...
    I used it in my game as well.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <iostream.h>
    #include <conio.c>
    
    int main()
    {
    
    for (int x=0;x>=0;x++)
    {
      time_t now;
      time(&now);
      
      printf("%s", ctime(&now));
      Sleep(100);
      clrscr();
      
    }
    return main();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Replies: 25
    Last Post: 10-28-2001, 01:08 PM