| View previous topic :: View next topic |
| Author |
Message |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Wed Jul 30, 2008 12:54 am Post subject: [Help] Getting a random number in C++ |
|
|
Could any1 please tell me, how i would get a random number, between 0 and 99 in C++?
Thanks for helping  |
|
| Back to top |
|
 |
Nuclear898 Grandmaster Cheater Supreme
Reputation: 0
Joined: 04 Jun 2006 Posts: 1597 Location: The Netherlands
|
Posted: Wed Jul 30, 2008 1:28 am Post subject: |
|
|
| 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 |
|
 |
FullyAwesome I post too much
Reputation: 0
Joined: 05 Apr 2007 Posts: 4438 Location: Land Down Under
|
Posted: Wed Jul 30, 2008 1:56 am Post subject: |
|
|
that is a lot different and a lot harder than vb.net lol  _________________
|
|
| Back to top |
|
 |
AndrewMan Grandmaster Cheater Supreme
Reputation: 0
Joined: 01 Aug 2007 Posts: 1257
|
Posted: Wed Jul 30, 2008 2:07 am Post subject: |
|
|
| fullyawesome wrote: | that is a lot different and a lot harder than vb.net lol  |
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Jul 30, 2008 2:30 am Post subject: |
|
|
| 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 |
|
 |
Nuclear898 Grandmaster Cheater Supreme
Reputation: 0
Joined: 04 Jun 2006 Posts: 1597 Location: The Netherlands
|
Posted: Wed Jul 30, 2008 3:32 am Post subject: |
|
|
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 |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Wed Jul 30, 2008 3:56 am Post subject: |
|
|
| 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 |
|
 |
Nuclear898 Grandmaster Cheater Supreme
Reputation: 0
Joined: 04 Jun 2006 Posts: 1597 Location: The Netherlands
|
Posted: Wed Jul 30, 2008 3:59 am Post subject: |
|
|
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 |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Jul 30, 2008 4:23 am Post subject: |
|
|
| 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 |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Wed Jul 30, 2008 7:32 am Post subject: |
|
|
Well, thanks that was what i needed!  |
|
| Back to top |
|
 |
GMZorita Grandmaster Cheater Supreme
Reputation: 0
Joined: 21 Mar 2007 Posts: 1361
|
Posted: Wed Jul 30, 2008 8:07 am Post subject: |
|
|
| 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 |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Jul 30, 2008 8:16 am Post subject: |
|
|
| GMZorita: I suggest inlining your function.. And "base" is a bit confusing, because we're talking about numbers.. |
|
| Back to top |
|
 |
GMZorita Grandmaster Cheater Supreme
Reputation: 0
Joined: 21 Mar 2007 Posts: 1361
|
Posted: Wed Jul 30, 2008 11:23 am Post subject: |
|
|
| 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 |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Wed Jul 30, 2008 2:23 pm Post subject: |
|
|
| 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 |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Wed Jul 30, 2008 2:25 pm Post subject: |
|
|
| 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 |
|
 |
|