Game Programming




 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [C++]Generate Random Number - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[C++]Generate Random Number (11th Jan 08 at 1:36am UTC)
Here i have posted what makes the computer generate a random number between the values that you have picked

Code:
 
  1. life = 5; // number of player lives
  2. srand((unsigned )time(NULL)); //init Rand() function
  3. j = rand() % maxrand; //j get a random value between 0 and maxrand
  4.  
  5. getreults();
  6.  
  7. }
  8.  
  9.  
  10. void getreults()
  11. {
  12. if(life <= 0)
  13. {
  14. std::cout << "You losen";
  15. system("cls");
  16. start();
  17. }
  18.  
  19. std::cout << "Type in a new number n";
  20. std:: cin >> i;
  21.  
  22.  
  23. if ((i>maxrand) || (i<0)) // if the user number isn't correct, restart
  24. {
  25. std::cout << "Error : Number not between 0 and n" << maxrand;
  26. getreults();
  27. }
  28.  
  29. if (i == j)
  30. {
  31. std::cout << "YOU WIN !nn"; // the user found the secret number
  32. start();
  33. }
  34.  
  35. else if (i>j)
  36. {
  37. std::cout << "Too BIGn";
  38. life = life - 1; // -1 to the user's "life"
  39. std::cout << "Number of remaining life: " << life << "nn";
  40. getreults();
  41. }
  42.  
  43. else if (i<j)
  44. {
  45. std::cout << "Too SMALLn";
  46. life = life - 1;
  47. std::cout << "Number of remaining life:n" << life << "nn";
  48. getreults();
  49. }
  50.  
  51.  
  52. }
  53.  
  54.  
 


As you see i haven't give you the whole code but i have given you enough to give you a understanding of how it is done

variables i used

maxrand
life
j
i
c
 Printable View

All times are GMT+0 :: The current time is 7:52am
Page generated in 0.0799 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums