| View previous topic :: View next topic |
| Author |
Message |
Engineer Expert Cheater
Reputation: 1
Joined: 25 Nov 2007 Posts: 170
|
Posted: Wed Jan 23, 2008 11:09 am Post subject: [c++] Randomnize an integer? |
|
|
| *insert title here*
|
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Wed Jan 23, 2008 11:13 am Post subject: Re: [c++] Randomnize an integer? |
|
|
_________________
Intel over amd yes. |
|
| Back to top |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Wed Jan 23, 2008 11:15 am Post subject: |
|
|
| Random()
|
|
| Back to top |
|
 |
Engineer Expert Cheater
Reputation: 1
Joined: 25 Nov 2007 Posts: 170
|
Posted: Wed Jan 23, 2008 12:20 pm Post subject: Re: [c++] Randomnize an integer? |
|
|
|
|
|
| Back to top |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Wed Jan 23, 2008 12:33 pm Post subject: |
|
|
| Code: | | yourinteger = Rand(1337) |
If Random is the same as Rand, then it will your integer to a number betwen 1~1337 or -1337~1337.
|
|
| Back to top |
|
 |
Engineer Expert Cheater
Reputation: 1
Joined: 25 Nov 2007 Posts: 170
|
Posted: Wed Jan 23, 2008 1:00 pm Post subject: |
|
|
| Code: | error:'random' cannot be used as a function;
error: the address of 'int rand()' will always evaluate as true;
|
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Jan 23, 2008 1:14 pm Post subject: |
|
|
| Code: | #include <time.h>
// or #include <ctime>
...
int iRandom = rand() % 10 + 1; |
Generates a Number 1 - 10 ( hence the +1, if that wasnt there it would generate 0 - 9 )
where rand() is the function
% MAX_RAND which is the highest array of numbers you want to generate
+ 1 just increases the number by 1 so you can have 1-100 instead of 0-99
Edit: but seriously, i googled this just now and found the first link
http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html
_________________
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Wed Jan 23, 2008 3:58 pm Post subject: |
|
|
Generating a random number with a given range:
Say we want to have a range of 15 min - 25 max.
The default would be
| Code: |
int ran = rand() % 26;
|
This will give us a range of 0 - 25. We will now shift it.
| Code: | | int ran = 15 + rand() % 26; |
This will give us a range of 15 - 40, but we want it to 25. To fix it, we just subtract it by 15.
| Code: | | int ran = 15 + rand() % (26 - 15); |
This will give use our desired range 15 - 25. With this, you can form an equation to use in a function
| Code: |
x = min
y = max
x + rand() % ((y + 1 ) - x)
|
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Jan 23, 2008 5:29 pm Post subject: |
|
|
using killersamurai's code up there, heres a simple function u can use
| Code: | int Randomize( int iRandomMin, int iRandomMax )
{
int iRandNumber = iRandomMin + rand() % ((iRandomMax + 1 ) - iRandomMin);
return iRandNumber;
} |
Example:
| Code: | #include <iostream>
using std::cout;
using std::cin;
int Randomize( int iRandomMin, int iRandomMax )
{
int iRandNumber = iRandomMin + rand() % ((iRandomMax + 1 ) - iRandomMin);
return iRandNumber;
}
int main()
{
int iMin, iMax;
cout << "-- Randomizor --\n" << "\nChoose Mininum Value: ";
cin >> iMin;
cout << "\nChoose Maxium Value: ";
cin >> iMax;
cout << "Random Number from those numbers: " << Randomize( iMin, iMax ) << "\n";
cin.sync();
cin.ignore();
return 0;
} |
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Jan 24, 2008 8:49 am Post subject: Re: [c++] Randomnize an integer? |
|
|
http://www.boost.org/libs/random/
| ZzOoZz wrote: | | Rand() is the function, but how do i set the int to change? | srand()
|
|
| Back to top |
|
 |
|