C++ Randint.. nucence
Author |
Message |
Geostigma
|
Posted: Thu May 17, 2007 10:35 pm Post subject: C++ Randint.. nucence |
|
|
I seriously fail to see why c++ does random int's the way it does.
std::rand()% 'x' picks a random number between 1 and like 22000 something crazy
this is my rand int program.
c: | float randint (int *num1, int num2 )
{
float num;
std:: srand(num2 );
*num1 = std:: rand();
num = *num1 + num2;
return num;
}
int main ()
{
int num1,num2 = 1;
std:: cout << "this is a random number generator";
std:: cout << randint (&num1,num1 );
std:: cout << "\n";
std:: cout << "";
std:: cin >> num1;
return 0;
}
|
apparently no matter what you do or even add the %32 to div the number and use its remainder or any other number it always spits out the same number over and over again and is never truly random. I have played around with this to no end and i feel like I'll never be able to commit this rand number to do specific numbers. I.e rand 1 to 10 vs 1 to 23000
an example I have was to tie a std::srand(intvariable) to a timer that runs off of a computer clock. Okay, thats not at all strange. C++ is really starting to frustrate me |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
md

|
Posted: Fri May 18, 2007 2:04 pm Post subject: RE:C++ Randint.. nucence |
|
|
srand() initiates the (relateivley poor) random number generator. rand)( returns a random number. Reseeding the randim number generator will make the next call to rand() return the same random number.
rand() doesn't really return a random number, it returns the next number is a psudorandom sequence. ideally if you want random numbers you seed the random number generator with the time, thus choosing a starting place in hte sequence that is different every time. After you seed the random number generator once you really don't ever need to seed it again.
Also, rand returns an integer iirc; so the float is kinda useless. |
|
|
|
|
 |
Geostigma
|
Posted: Fri May 18, 2007 8:01 pm Post subject: RE:C++ Randint.. nucence |
|
|
Yeah good point on the float. I don't understand why I should have to tie it to time. Your basically telling me the same thing in the book I have. I want to be very specific in the range I want to call my rand int but it doesn't seem all that possible. Like how would you randomize a true false statement with rand ? Or if I do a math game and I don't want questions that are like 1435+4574 or 12x1354135.
I know I'm getting extreme with my example and probably using the "%" to lower it to tens but thats about it. I do understand why I have to use time but thats still super limited I guess but not really "random" because if you were using seconds and kept count of the time then it would be in your best interests or work for you |
|
|
|
|
 |
Mazer

|
Posted: Fri May 18, 2007 8:17 pm Post subject: RE:C++ Randint.. nucence |
|
|
When you have "rand() % foo" you're going to get some number in the range of 0 to foo - 1 inclusive.
You use the time as a random seed because nobody is going to run your program multiple times per second. No, it's not random; you can't get random with a computer. But I'm sure this will be good enough. |
|
|
|
|
 |
octopi

|
Posted: Fri May 18, 2007 8:17 pm Post subject: RE:C++ Randint.. nucence |
|
|
You should be able to use modulo with any number to set a range, you want numbers 0-9 use modulo 10, you want 0-15 use modulo 16 etc...
Its not really tieing it to time, its just initializing the random numbers, it doesn't matter what you pass it, and you only call it once. Usually you'll call it in your main sub at the start, then from then on you'd just call rand(), also you never use num2 in your main sub
So basically remove this line from your randint sub
std::srand(num2);
and call that from your main instead, but change num2 to something thats more dynamic (aka time)
You can't get truely random numbers from a computer, you need to use some outside phenonenom to get them, I know one website that uses atmospheric noise to generate random numbers. |
|
|
|
|
 |
Geostigma
|
Posted: Fri May 18, 2007 8:58 pm Post subject: RE:C++ Randint.. nucence |
|
|
Yeah I have issues about that modulo thing is that I don't get a number from what I specified for "foo" |
|
|
|
|
 |
md

|
Posted: Fri May 18, 2007 10:35 pm Post subject: RE:C++ Randint.. nucence |
|
|
rand() returns a number in the range of 0-2^32-1
If your looking for a number in a specific range you need to massage the result rand gives you. To get a number within a specific range you could use something similar to
c++: |
int upper=15, lower=5;
int random_number = lower+(rand()%(upper-lower));
|
I recommend reading about random number generators before you use them, learn how they work and why they aren't really random. Wikipedia is actually not a bad source for a basic understanding.
[edit]fixed rand -> rand() |
|
|
|
|
 |
Geostigma
|
Posted: Fri May 18, 2007 10:50 pm Post subject: RE:C++ Randint.. nucence |
|
|
learning, + reading + more reading. Something tells me c++ is more theory then practical application atm. Ill take a looksee |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Martin

|
Posted: Mon May 28, 2007 3:26 pm Post subject: RE:C++ Randint.. nucence |
|
|
Computer science is about theory. The science part of the name should tip you off. As for application, C, C++, C# and Java (C++ being close to a superset of C, the latter two heavily influenced by C and C++ ) make up the vast majority of commercially used programming languages (for better or for worse).
Anyway, if you want to be a computer scientist, don't be scared of learning. Pretend you like math.  |
|
|
|
|
 |
wtd
|
Posted: Tue May 29, 2007 2:34 am Post subject: RE:C++ Randint.. nucence |
|
|
And learn lots of programming languages! |
|
|
|
|
 |
|
|