| View previous topic :: View next topic |
| Author |
Message |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 7:13 pm Post subject: [C++]Random Values |
|
|
is there a way to initialze a variable with a randomly generated value? i wanted the value to be randomly generated within a range of 1 to 100 also... how could i do this?
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Mon Jan 28, 2008 8:13 pm Post subject: |
|
|
| Code: | srand(GetTickCount());
int RandomNumber = rand()%100; |
|
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 8:22 pm Post subject: |
|
|
i started C++ yesterday so can you explain what the top line does exactly? and since its value is going to be limited to 1 to 100 anyways, couldn't you declare RandomNumber as unsigned char(0 to 255)?
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Mon Jan 28, 2008 8:26 pm Post subject: |
|
|
srand
As for the rand()%100, that would provide a value from 0-99.
If you caught on, rand()%10, would be 0-9 and so on.
rand()%732 : 0-731
|
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Mon Jan 28, 2008 8:32 pm Post subject: |
|
|
thanks a lot. but you didnt anwser my question about data type.
EDIT:
| Code: | | srand(GetTickCount()); |
i really have no idea what to do with that.
| Code: | | int RandomNumber = rand()%100; |
i think i got this part. initialize it as a variable... right?
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jan 28, 2008 10:28 pm Post subject: |
|
|
| guernica wrote: | thanks a lot. but you didnt anwser my question about data type.
EDIT:
| Code: | | srand(GetTickCount()); |
i really have no idea what to do with that.
| Code: | | int RandomNumber = rand()%100; |
i think i got this part. initialize it as a variable... right? |
srand sets a starting point for the generation of a series of psesudorandom numbers (integers). The param is a seed to randomize the starting point of where the generator will start generating random numbers. Without calling srand() with a random number, you will always get the same numbers.
Also, if you pass 1 as the seed, you only reinit the generator which again will give you the same numbers again. So be sure to pass a random number. Which is why GetTickCount() was used in the example given to you.
GetTickCount returns the number of milliseconds the system has been running for. Which is constantly updating. Just call srand(GetTickCount()) before you call rand() and it will do the work for ya.
And yes, you need to init your variable to hold the value of the random number. You can make a quick function such as this to create a random number:
| Code: | #include <windows.h>
#include <iostream>
int RandomNumber()
{
return rand()%100;
}
int main()
{
srand( GetTickCount() );
for( int x=0; x<10; x++ )
std::cout << RandomNumber() << std::endl;
std::cin.ignore();
std::cin.sync();
return ERROR_SUCCESS;
}
|
_________________
- Retired. |
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Tue Jan 29, 2008 12:30 am Post subject: |
|
|
| Code: | int main()
{
srand( GetTickCount() );
//allocation of vars
int guess = 0;
int guessamount = 0;
int randnumber = rand()%100;
//introduction
cout << "Welcome to the Guessing Game." << endl << "The Rules are simple, I will think of a number from 1 to 100." << endl << "You will try to guess it.";
cout << randomnumber;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guess;
if (guess < randomnumber)
cout << "Incorrect guess, please guess higher."; |
i get a undeclared identifier error for randomnumber when i try to compile this to test it. what can i do so the random value determined in the beginning of the program remains consistent throughout the rest of the program, but still generates a new value every time its executed?[/code]
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Jan 29, 2008 12:58 am Post subject: |
|
|
| guernica wrote: |
i get a undeclared identifier error for randomnumber when i try to compile this to test it. what can i do so the random value determined in the beginning of the program remains consistent throughout the rest of the program, but still generates a new value every time its executed?[/code] |
You declared "randnumber" not "randomnumber", but you tried to use it anyway. Also you weren't seeding rand() so you would just end up with the same number every time.
| Code: | #include <windows.h>
#include <cstdlib>
#include <iostream>
int g;
int r;
int main(void)
{
srand(GetTickCount());
r = rand() % 101;
do{
std::cout << "Guess a number, 0-100.\n";
std::cin >> g;
if (r < g){
std::cout << "Lower.\n";
}
else if (r > g){
std::cout << "Higher.\n";
}
}while (g != r);
std::cout << "You got it.";
return 0;
} |
|
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Tue Jan 29, 2008 1:19 am Post subject: |
|
|
my add got the best of me. and its different every time. i tested it. thanks for pointing out the stupidest fucking mistake that i could ever make.
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
|