Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]Random Values

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
guernica
Grandmaster Cheater Supreme
Reputation: 0

Joined: 20 Jun 2007
Posts: 1211

PostPosted: Mon Jan 28, 2008 7:13 pm    Post subject: [C++]Random Values Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
Renkokuken
GO Moderator
Reputation: 4

Joined: 22 Oct 2006
Posts: 3249

PostPosted: Mon Jan 28, 2008 8:13 pm    Post subject: Reply with quote

Code:
   srand(GetTickCount());
   int RandomNumber = rand()%100;
Back to top
View user's profile Send private message
guernica
Grandmaster Cheater Supreme
Reputation: 0

Joined: 20 Jun 2007
Posts: 1211

PostPosted: Mon Jan 28, 2008 8:22 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Mon Jan 28, 2008 8:24 pm    Post subject: Reply with quote

already a posted topic on EXACTLY the same

http://forum.cheatengine.org/viewtopic.php?t=190223

a simple function i made:
http://forum.cheatengine.org/viewtopic.php?p=1946607#1946607

_________________
Back to top
View user's profile Send private message
Renkokuken
GO Moderator
Reputation: 4

Joined: 22 Oct 2006
Posts: 3249

PostPosted: Mon Jan 28, 2008 8:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
guernica
Grandmaster Cheater Supreme
Reputation: 0

Joined: 20 Jun 2007
Posts: 1211

PostPosted: Mon Jan 28, 2008 8:32 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Jan 28, 2008 10:28 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
guernica
Grandmaster Cheater Supreme
Reputation: 0

Joined: 20 Jun 2007
Posts: 1211

PostPosted: Tue Jan 29, 2008 12:30 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Tue Jan 29, 2008 12:58 am    Post subject: Reply with quote

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
View user's profile Send private message
guernica
Grandmaster Cheater Supreme
Reputation: 0

Joined: 20 Jun 2007
Posts: 1211

PostPosted: Tue Jan 29, 2008 1:19 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites