 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Micah Grandmaster Cheater
Reputation: 1
Joined: 30 Mar 2007 Posts: 833
|
Posted: Mon Aug 04, 2008 12:37 pm Post subject: [C++ Help] Choose a number from a list randomly |
|
|
How could I make a C++ program randomly choose 1 number from 5 numbers?
The 5 numbers would not be 1-5 they would be more like 12, 18, 34, 48, 7.
I just need to know how to get it to randomly display the numbers I want it to.
Thanks for any help. _________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Aug 04, 2008 12:44 pm Post subject: |
|
|
As you know, if the numbers are in a listbox, each member has a number. 1-5
So calculate a random number 1-5 or 0-4, however it is labeled, and if rand does not return an even number, round up or down.
Then select that member number _________________
|
|
| Back to top |
|
 |
Micah Grandmaster Cheater
Reputation: 1
Joined: 30 Mar 2007 Posts: 833
|
Posted: Mon Aug 04, 2008 1:08 pm Post subject: |
|
|
I have never made anything in C++ before, however I have read up on it and watched alot of tutorial videos, so do I assign all of the values to an array and then have the rand function come up with a random array number?
A code example would be greatly appreciated. _________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Aug 04, 2008 3:23 pm Post subject: |
|
|
Something like this?
| Code: | #include <windows.h>
#include <time.h>
#include <iostream>
int iArray[] = { 1, 35, 643, 24, 62 };
int main( int argc, TCHAR* argv[] )
{
srand( time( 0 ) );
std::cout << "Random number picked: " << iArray[ rand()%6 ] << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
} |
_________________
- Retired. |
|
| Back to top |
|
 |
Micah Grandmaster Cheater
Reputation: 1
Joined: 30 Mar 2007 Posts: 833
|
Posted: Mon Aug 04, 2008 4:10 pm Post subject: |
|
|
Ok thanks for that
When I compile my stuff I can't run it! What am I doing wrong?
I have tried it on DevC++ and Visual C++
Oh and can you tell me what
| Code: | | int argc, TCHAR* argv[] | means/does
and
means / does
means/does
means/does
Thank you for all the help, im still learning all of the syntax for this language  _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon Aug 04, 2008 4:18 pm Post subject: |
|
|
argc and argv are command line arguments.
| Quote: |
In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.
The integer, argc is the ARGument Count (hence argc). It is the number of arguments passed into the program from the command line, including the name of the program.
The array of character pointers is the listing of all the arguments. argv[0] is the name of the program, or an empty string if the name is not available. After that, every element number less than argc are command line arguments.
|
srand(time(0)) is setting a seed for the rand function. And rand generates a random number using the seed. the %6 just means that it will only generate numbers 0-5.
std:: is used because there's no point in using the whole standard namespace in this little application, so whenever you reference something in the standard namespace, since you haven't declared your using it (using namespace std;) you have to put std::. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Micah Grandmaster Cheater
Reputation: 1
Joined: 30 Mar 2007 Posts: 833
|
Posted: Mon Aug 04, 2008 4:28 pm Post subject: |
|
|
| Quote: | | the %6 just means that it will only generate numbers 0-5. |
0-5 on the array or 0-5 on the number line (actual numbers and not what I have assigned to 0-5 on the array) _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon Aug 04, 2008 4:52 pm Post subject: |
|
|
Well it would be 0-5 numberswise. But since Wiccaan placed the rand function inside the brackets, the number generated would be the index of an element in the array. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Micah Grandmaster Cheater
Reputation: 1
Joined: 30 Mar 2007 Posts: 833
|
Posted: Mon Aug 04, 2008 5:52 pm Post subject: |
|
|
WOO!
KK that works fine
Now I want to do this but my syntax is off still
Help would be greatly appreciated!
| Code: | #include <windows.h>
#include <time.h>
#include <iostream>
int iArray[] = { 18, 17, 9, 2, 3, 5, 6, 8, 12, 19, 20, 24, 26, 37 };
int iArraypt2[] = { 7, 8, 9, 13, 18, 25, 30, 44, 49, 52};
int iArraypt3[] = { 2, 4, 14, 14, 19, 22, 28, 29, 32, 34, 39, 40, 46, 47, 51};
int iArraypt1[] = { 12, 20, 23, 11, 16, 21, 31, 37, 48, 55};
int main( int argc, TCHAR* argv[] )
{
srand( time( 0 ) );
std::cout << "l number is: " << iArray[ rand()%15 ]<<
cout << " number is: " << iArraypt1[ rand()11%]<<
std::cout << iArraypt2[ rand()11%] << iArraypt3[ rand()16%]<<
std::cout << iArraypt3[ rand()16%] <<iArraypt3[ rand()16%] << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
srand( time( 0 ) );
} |
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Aug 04, 2008 6:29 pm Post subject: |
|
|
| Micah wrote: | WOO!
KK that works fine
Now I want to do this but my syntax is off still
Help would be greatly appreciated!
| Code: | #include <windows.h>
#include <time.h>
#include <iostream>
int iArray[] = { 18, 17, 9, 2, 3, 5, 6, 8, 12, 19, 20, 24, 26, 37 };
int iArraypt2[] = { 7, 8, 9, 13, 18, 25, 30, 44, 49, 52};
int iArraypt3[] = { 2, 4, 14, 14, 19, 22, 28, 29, 32, 34, 39, 40, 46, 47, 51};
int iArraypt1[] = { 12, 20, 23, 11, 16, 21, 31, 37, 48, 55};
int main( int argc, TCHAR* argv[] )
{
srand( time( 0 ) );
std::cout << "l number is: " << iArray[ rand()%15 ]<<
cout << " number is: " << iArraypt1[ rand()11%]<<
std::cout << iArraypt2[ rand()11%] << iArraypt3[ rand()16%]<<
std::cout << iArraypt3[ rand()16%] <<iArraypt3[ rand()16%] << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
srand( time( 0 ) );
} |
|
Firstly, do not call srand() more the once. You only need it once. Along with that, only call it at the start of your code like it is already. Remove the second occurrence.
Next, your syntax is off cause you are trying to do multiple lines for things that only need to be one line. Your code that is like this is incorrect:
| Code: | std::cout << "l number is: " << iArray[ rand()%15 ]<<
cout << " number is: " << iArraypt1[ rand()11%]<<
std::cout << iArraypt2[ rand()11%] << iArraypt3[ rand()16%]<<
std::cout << iArraypt3[ rand()16%] <<iArraypt3[ rand()16%] << std::endl; |
Write things out one line at a time. Looking at that, I can't really tell what you are trying to do with it. I would assume you are just trying to print out some various numbers from each array. So I would assume something like this would better suite your code:
| Code: | #include <windows.h>
#include <time.h>
#include <iostream>
int iArray[] = { 18, 17, 9, 2, 3, 5, 6, 8, 12, 19, 20, 24, 26, 37 };
int iArraypt2[] = { 7, 8, 9, 13, 18, 25, 30, 44, 49, 52};
int iArraypt3[] = { 2, 4, 14, 14, 19, 22, 28, 29, 32, 34, 39, 40, 46, 47, 51};
int iArraypt1[] = { 12, 20, 23, 11, 16, 21, 31, 37, 48, 55};
int main( int argc, TCHAR* argv[] )
{
// Randomize Numbers For rand()
srand( time( 0 ) );
// Output Number Guesses
std::cout << "Number 1: " << iArray[ rand()%15 ] << std::endl
<< "Number 2: " << iArraypt1[ rand()%11 ] << std::endl
<< "Number 3: " << iArraypt2[ rand()%11 ] << std::endl
<< "Number 4: " << iArraypt3[ rand()%16 ] << std::endl
<< "Number 5: " << iArraypt3[ rand()%16 ] << std::endl
<< "Number 6: " << iArraypt3[ rand()%16 ] << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
} |
Some other mistakes you had, for one, you were using rand() incorrectly. Your code was attempting to use it by doing: rand()15% for example. Instead, the % should be BEFORE the number like this: rand()%15
You also tried doing cout instead of std::cout, I personally don't use the namespace. Instead, you need to use std::cout if you do not use the namespace. You can't jump between the two.
To answer your above questions:
| Code: | | int argc, TCHAR* argv[] |
These are for command line arguments in a console application. argc holds the number of arguments currently passed to the program at startup, and argv is an array that holds each argument.
You can read up about it on the MSDN documentation:
http://msdn.microsoft.com/en-us/library/f0d4wb4t(VS.80).aspx
std is a namespace. You can read more about them here:
http://www.cplusplus.com/doc/tutorial/namespaces.html
rand itself is a run-time library function. Read about it here:
http://msdn.microsoft.com/en-us/library/398ax69y.aspx
When you add a %<number> you are creating a math equation. % means modulo, or a remainder of a percentage. Doing the above will give you a returned value between 0 and the stated number. _________________
- Retired. |
|
| Back to top |
|
 |
Micah Grandmaster Cheater
Reputation: 1
Joined: 30 Mar 2007 Posts: 833
|
Posted: Mon Aug 04, 2008 7:30 pm Post subject: |
|
|
Is there a way to display numbers 2-6 side by side? _________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Aug 04, 2008 8:04 pm Post subject: |
|
|
Just do something like:
| Code: | | std::cout << iArray[ 2 ] << " " << iArray[ 6 ] << std::endl; |
_________________
- Retired. |
|
| Back to top |
|
 |
chaseC Newbie cheater
Reputation: 0
Joined: 08 Jul 2008 Posts: 10
|
Posted: Mon Aug 04, 2008 8:05 pm Post subject: |
|
|
| nevermind |
|
| Back to top |
|
 |
Micah Grandmaster Cheater
Reputation: 1
Joined: 30 Mar 2007 Posts: 833
|
Posted: Mon Aug 04, 2008 8:45 pm Post subject: |
|
|
No, I wanted to display
| Code: |
int iArraypt2[]
int iArraypt3[]
int iArraypt1[]
|
I want to display the random number gathered from those side by side
Thanks for all your help Wiccan!
| Code: | #include <windows.h>
#include <time.h>
#include <iostream>
int iArray[] = { 18, 17, 9, 2, 3, 5, 6, 8, 12, 19, 20, 24, 26, 37 };
int iArraypt2[] = { 7, 8, 9, 13, 18, 25, 30, 44, 49, 52};
int iArraypt3[] = { 2, 4, 14, 14, 19, 22, 28, 29, 32, 34, 39, 40, 46, 47, 51};
int iArraypt1[] = { 12, 20, 23, 11, 16, 21, 31, 37, 48, 55};
int main( int argc, TCHAR* argv[] )
{
srand( time( 0 ) );
std::cout << "Your powerball number is: " << iArray[ rand()%15 ] << std::endl;
<< "Your number: " << iArraypt1[ rand()%11 ] << std::endl;
<< "" << iArraypt2[ rand()%11 ] << std::endl
<< "" << iArraypt3[ rand()%16 ] << std::endl
<< "" << iArraypt3[ rand()%16 ] << std::endl
<< "" << iArraypt3[ rand()%16 ] << std::endl;
std::cin.sync();
std::cin.ignore();
return 0;
}
|
That still gives me one error and I have been trying alot of different things to get it to work, if you can just help me get that working it should be good. _________________
|
|
| Back to top |
|
 |
chaseC Newbie cheater
Reputation: 0
Joined: 08 Jul 2008 Posts: 10
|
Posted: Mon Aug 04, 2008 8:50 pm Post subject: |
|
|
you mean like this?
| Code: | #include <windows.h>
#include <time.h>
#include <iostream>
int iArray[] = { 18, 17, 9, 2, 3, 5, 6, 8, 12, 19, 20, 24, 26, 37 };
int iArraypt2[] = { 7, 8, 9, 13, 18, 25, 30, 44, 49, 52};
int iArraypt3[] = { 2, 4, 14, 14, 19, 22, 28, 29, 32, 34, 39, 40, 46, 47, 51};
int iArraypt1[] = { 12, 20, 23, 11, 16, 21, 31, 37, 48, 55};
int main( int argc, TCHAR* argv[] )
{
srand( time( 0 ) );
std::cout << "Your powerball number is: " << iArray[ rand()%15 ] << std::endl
<< "Your number: " << iArraypt1[ rand()%11 ]
<< " " << iArraypt2[ rand()%11 ]
<< " " << iArraypt3[ rand()%16 ]
<< " " << iArraypt3[ rand()%16 ]
<< " " << iArraypt3[ rand()%16 ];
std::cin.sync();
std::cin.ignore();
return 0;
}
|
|
|
| Back to top |
|
 |
|
|
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
|
|