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 


[Help] Getting a random number in C++
Goto page 1, 2  Next
 
Post new topic   This topic is locked: you cannot edit posts or make replies.    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Wed Jul 30, 2008 12:54 am    Post subject: [Help] Getting a random number in C++ Reply with quote

Could any1 please tell me, how i would get a random number, between 0 and 99 in C++?

Thanks for helping Very Happy
Back to top
View user's profile Send private message
Nuclear898
Grandmaster Cheater Supreme
Reputation: 0

Joined: 04 Jun 2006
Posts: 1597
Location: The Netherlands

PostPosted: Wed Jul 30, 2008 1:28 am    Post subject: Reply with quote

Code:
#include <iostream>
#include <windows.h>

int main()
{
srand(GetTickCount()); //so our randomized numbers aren't the same everytime we start the program
int randomnumber = rand() % 100; //randomize numbers from 0-99, you can make it 1-100 with rand() % 100 + 1;
std::cout << randomnumber << std::endl; //display our randomnumber in the console
std::cin.get(); //pause the program untill a key is pressed
main(); //start our main() function from the top again
}


Hope you get it :X
Back to top
View user's profile Send private message
FullyAwesome
I post too much
Reputation: 0

Joined: 05 Apr 2007
Posts: 4438
Location: Land Down Under

PostPosted: Wed Jul 30, 2008 1:56 am    Post subject: Reply with quote

that is a lot different and a lot harder than vb.net lol Laughing
_________________
Back to top
View user's profile Send private message MSN Messenger
AndrewMan
Grandmaster Cheater Supreme
Reputation: 0

Joined: 01 Aug 2007
Posts: 1257

PostPosted: Wed Jul 30, 2008 2:07 am    Post subject: Reply with quote

fullyawesome wrote:
that is a lot different and a lot harder than vb.net lol Laughing


Of course it is.

C++ is like... a different language

VB.net is like... English, you just have to know what you're doing, or else vb.net can be confusing.

_________________
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Wed Jul 30, 2008 2:30 am    Post subject: Reply with quote

Nuclear898 wrote:
Code:
#include <iostream>
#include <windows.h>

int main()
{
srand(GetTickCount()); //so our randomized numbers aren't the same everytime we start the program
int randomnumber = rand() % 100; //randomize numbers from 0-99, you can make it 1-100 with rand() % 100 + 1;
std::cout << randomnumber << std::endl; //display our randomnumber in the console
std::cin.get(); //pause the program untill a key is pressed
main(); //start our main() function from the top again
}


Hope you get it :X


Calling main like this is a terrible idea, just loop.

edit: The compiler will throw a warning about it being retarded anyway.
Back to top
View user's profile Send private message
Nuclear898
Grandmaster Cheater Supreme
Reputation: 0

Joined: 04 Jun 2006
Posts: 1597
Location: The Netherlands

PostPosted: Wed Jul 30, 2008 3:32 am    Post subject: Reply with quote

Yea well that's not the issue, he just wanted to know about the srand() and rand() functions, I just wrote a small program in 1 minute to show that it randomizes a different number anytime.

You mean loop like this?

Code:
#include <iostream>
#include <windows.h>

int main()
{
   for (;;)
   {
      srand(GetTickCount());
      int randomnumber = rand() % 100; + 1;
      std::cout << randomnumber << std::endl;
      std::cin.get();
   }
}
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Wed Jul 30, 2008 3:56 am    Post subject: Reply with quote

Code:
#include <iostream>
#include <ctime>

int main( void )
{
   srand( (unsigned int)time(NULL) );
   int Rand = rand() % 99;

   std::cout << "Random Num: " << Rand;

   return 0;
}


Nuclear898, srand only needs to be called once. More like this:
Code:
#include <iostream>
#include <windows.h>

int main()
{
   int randomnumber;
   srand(GetTickCount());

   for (;;Sleep(1000))
   {
      randomnumber = rand() % 100; + 1;
      std::cout << randomnumber << std::endl;
   }

   return 0;
}
Back to top
View user's profile Send private message MSN Messenger
Nuclear898
Grandmaster Cheater Supreme
Reputation: 0

Joined: 04 Jun 2006
Posts: 1597
Location: The Netherlands

PostPosted: Wed Jul 30, 2008 3:59 am    Post subject: Reply with quote

Oh yeah slight mistake there :X

I used time too but when you randomize and record the random numbers with short intervals, it will give the same randomized numbers during a short period of time.

It's funny how in almost every thread, the answer to OP's question is answered within 1-3 posts, but people keep posting relevant additions in the same thread to the original topic.
I think I'll visit this section more often, much better than a tarded section like Maplestory.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Wed Jul 30, 2008 4:23 am    Post subject: Reply with quote

Nuclear898 wrote:
I used time too but when you randomize and record the random numbers with short intervals, it will give the same randomized numbers during a short period of time.
Do NOT call ::srand() everytime you want to get a new random number. Also, remember to state that you're using 32-bit time if you're on 32-bit system.
Code:
#ifdef _WIN32
#define _USE_32BIT_TIME_T
#endif

#include <iostream>
#include <ctime>
#include <windows.h>

void Print( char *t, int *arr, int l );

int main( int argc, char *argv[] )
{
   int r[3];

   for(int i=0; i<(sizeof(r)/sizeof(int)); ++i) {
      ::srand( ::GetTickCount() );
      r[i] = ::rand();
   }
   Print( "srand everytime with GTC", r, sizeof(r)/sizeof(int) );

   ::srand( ::GetTickCount() );
   for(int i=0; i<(sizeof(r)/sizeof(int)); ++i)
      r[i] = ::rand();
   Print( "srand once with GTC", r, sizeof(r)/sizeof(int) );

   for(int i=0; i<(sizeof(r)/sizeof(int)); ++i) {
      ::srand( ::time(NULL) );
      r[i] = ::rand();
   }
   Print( "srand everytime with time", r, sizeof(r)/sizeof(int) );

   ::srand( ::time(NULL) );
   for(int i=0; i<(sizeof(r)/sizeof(int)); ++i)
      r[i] = ::rand();
   Print( "srand once with time", r, sizeof(r)/sizeof(int) );

   return EXIT_SUCCESS;
}

void Print( char *t, int *arr, int l ) {
   std::cout << t << ":" << std::endl;
   for(int i=0; i<l; ++i)
      std::cout << arr[i] << std::endl;
}
Output:
Code:
srand everytime with GTC:
12891
12891
12891
srand once with GTC:
12891
18634
1285
srand everytime with time:
14733
14733
14733
srand once with time:
14733
10988
11003


EDIT: Cleaned the code a bit.
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Wed Jul 30, 2008 7:32 am    Post subject: Reply with quote

Well, thanks Wink that was what i needed! Very Happy
Back to top
View user's profile Send private message
GMZorita
Grandmaster Cheater Supreme
Reputation: 0

Joined: 21 Mar 2007
Posts: 1361

PostPosted: Wed Jul 30, 2008 8:07 am    Post subject: Reply with quote

Code:

int genrandnumeber(int max,int base)
{
return (rand() % max) + base);
}


eg: int a = genrandnumber(89,10);
will gen a number from 10 to 99.

_________________
Gone
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Wed Jul 30, 2008 8:16 am    Post subject: Reply with quote

GMZorita: I suggest inlining your function.. And "base" is a bit confusing, because we're talking about numbers..
Back to top
View user's profile Send private message
GMZorita
Grandmaster Cheater Supreme
Reputation: 0

Joined: 21 Mar 2007
Posts: 1361

PostPosted: Wed Jul 30, 2008 11:23 am    Post subject: Reply with quote

Jani wrote:
GMZorita: I suggest inlining your function.. And "base" is a bit confusing, because we're talking about numbers..

I not even looked at it i just did one rlly fast. and yeah sure why not inline.

Code:

int getrandnumber(int max,int min) {return (rand() % max) + base)};

_________________
Gone
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Wed Jul 30, 2008 2:23 pm    Post subject: Reply with quote

Flyte wrote:
GMZorita wrote:
Jani wrote:
GMZorita: I suggest inlining your function.. And "base" is a bit confusing, because we're talking about numbers..

I not even looked at it i just did one rlly fast. and yeah sure why not inline.

Code:

int getrandnumber(int max,int min) {return (rand() % max) + base)};


Ahahahahahaha.


inline != oneline

_________________
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Wed Jul 30, 2008 2:25 pm    Post subject: Reply with quote

GMZorita wrote:
Jani wrote:
GMZorita: I suggest inlining your function.. And "base" is a bit confusing, because we're talking about numbers..

I not even looked at it i just did one rlly fast. and yeah sure why not inline.

Code:

int getrandnumber(int max,int min) {return (rand() % max) + base)};


Why would there be a ";" at the end of a function? You didn't define "base". You forget to add a ";" after your function/api call -.-

P.S Writing a function in one line doesn't make you look pro.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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