# Random Generator Help

[green]
[code]
rand();
[/code]
is the only random generator I know so could you help me with my question to truly generate a completely random number?
[/green]

• (some parts are from MSDN)
[code]
#include // for getch()
[blue]#include // for rand()
#include // for time()[/blue]
#include

using namespace std;

int main()
{

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run
*/

[blue] srand( (unsigned)time( NULL ) );[/blue]

// Display 10 numbers
for( int i=0; i<10; i++ )
cout << [blue]rand()%100[/blue] << endl;
// with '%' operator, you can limit the number of
// digits that is generated : %10 -> 0~9, %100 -> 0~99 ...

getch(); // pause

return 0;
}
[/code]
[hr]
;-)
• :
: (some parts are from MSDN)
: [code]
: #include // for getch()
: [blue]#include // for rand()
: #include // for time()[/blue]
: #include
:
:
: using namespace std;
:
: int main()
: {
:
: /* Seed the random-number generator with current time so that
: * the numbers will be different every time we run
: */
:
: [blue] srand( (unsigned)time( NULL ) );[/blue]
:
: // Display 10 numbers
: for( int i=0; i<10; i++ )
: cout << [blue]rand()%100[/blue] << endl;
: // with '%' operator, you can limit the number of
: // digits that is generated : %10 -> 0~9, %100 -> 0~99 ...
:
: getch(); // pause
:
: return 0;
: }
: [/code]
: [hr]
: ;-)
:

• : :
: : (some parts are from MSDN)
: : [code]
: : #include // for getch()
: : [blue]#include // for rand()
: : #include // for time()[/blue]
: : #include
: :
: :
: : using namespace std;
: :
: : int main()
: : {
: :
: : /* Seed the random-number generator with current time so that
: : * the numbers will be different every time we run
: : */
: :
: : [blue] srand( (unsigned)time( NULL ) );[/blue]
: :
: : // Display 10 numbers
: : for( int i=0; i<10; i++ )
: : cout << [blue]rand()%100[/blue] << endl;
: : // with '%' operator, you can limit the number of
: : // digits that is generated : %10 -> 0~9, %100 -> 0~99 ...
: :
: : getch(); // pause
: :
: : return 0;
: : }
: : [/code]
: : [hr]
: : ;-)
: :
:
:
: Reading any C/C++ FAQ would also have told him to use srand(). Google is your friend.
:
I was reading my C++ book and it said to use the [code] rand(); [/code] insted of the [code]srand();[/code]
• If you don't use 'srand', every run of your app, will show same sequences of random numbers ([red]pseudo_random numbers[/red])! then use 'srand' to make 'rand', generates every_run_random numbers ;-)
• : : :
: : : (some parts are from MSDN)
: : : [code]
: : : #include // for getch()
: : : [blue]#include // for rand()
: : : #include // for time()[/blue]
: : : #include
: : :
: : :
: : : using namespace std;
: : :
: : : int main()
: : : {
: : :
: : : /* Seed the random-number generator with current time so that
: : : * the numbers will be different every time we run
: : : */
: : :
: : : [blue] srand( (unsigned)time( NULL ) );[/blue]
: : :
: : : // Display 10 numbers
: : : for( int i=0; i<10; i++ )
: : : cout << [blue]rand()%100[/blue] << endl;
: : : // with '%' operator, you can limit the number of
: : : // digits that is generated : %10 -> 0~9, %100 -> 0~99 ...
: : :
: : : getch(); // pause
: : :
: : : return 0;
: : : }
: : : [/code]
: : : [hr]
: : : ;-)
: : :
: :
: :
: : Reading any C/C++ FAQ would also have told him to use srand(). Google is your friend.
: :
: I was reading my C++ book and it said to use the [code] rand(); [/code] insted of the [code]srand();[/code]
:

What book are you using?

If your book tells you to use rand() [italic]instead of[/italic] srand(), the author doesn't even know the language, and I would sincerely recommend you find another book.

srand SEEDS rand. That is, pseudo_random numbers are created by using mathmatical equations on a single or multiple seed(s) to produce a very random series of numbers (well, random-looking, if you don't know the seed). The same seed always makes the same series of numbers.

srand() isn't something you use instead of rand(), it MUST be used WITH rand(), and there is no way to avoid this.

Hope it helps!
Sincerely,
Jakykong (Jack Mudge)
jack_mudge@hotmail.com

• : : : :
: : : : (some parts are from MSDN)
: : : : [code]
: : : : #include // for getch()
: : : : [blue]#include // for rand()
: : : : #include // for time()[/blue]
: : : : #include
: : : :
: : : :
: : : : using namespace std;
: : : :
: : : : int main()
: : : : {
: : : :
: : : : /* Seed the random-number generator with current time so that
: : : : * the numbers will be different every time we run
: : : : */
: : : :
: : : : [blue] srand( (unsigned)time( NULL ) );[/blue]
: : : :
: : : : // Display 10 numbers
: : : : for( int i=0; i<10; i++ )
: : : : cout << [blue]rand()%100[/blue] << endl;
: : : : // with '%' operator, you can limit the number of
: : : : // digits that is generated : %10 -> 0~9, %100 -> 0~99 ...
: : : :
: : : : getch(); // pause
: : : :
: : : : return 0;
: : : : }
: : : : [/code]
: : : : [hr]
: : : : ;-)
: : : :
: : :
: : :
: : : Reading any C/C++ FAQ would also have told him to use srand(). Google is your friend.
: : :
: : I was reading my C++ book and it said to use the [code] rand(); [/code] insted of the [code]srand();[/code]
: :
:
:
: What book are you using?
:
: If your book tells you to use rand() [italic]instead of[/italic] srand(), the author doesn't even know the language, and I would sincerely recommend you find another book.
:
: srand SEEDS rand. That is, pseudo_random numbers are created by using mathmatical equations on a single or multiple seed(s) to produce a very random series of numbers (well, random-looking, if you don't know the seed). The same seed always makes the same series of numbers.
:
: srand() isn't something you use instead of rand(), it MUST be used WITH rand(), and there is no way to avoid this.
:
:
: Hope it helps!
: Sincerely,
: Jakykong (Jack Mudge)
: jack_mudge@hotmail.com
:
:
[blue]
Just wanted to add: if you do not use srand(), rand() will almost (if not always) return the [b]same[/b] value because it is using the same generated number all the time.

srand() generates another number for rand() to use, hence creates truely random numbers.

srand() and rand() are two different routines that work with each other to produce random numbers.
[/blue]
• : : : : :
: : : : : (some parts are from MSDN)
: : : : : [code]
: : : : : #include // for getch()
: : : : : [blue]#include // for rand()
: : : : : #include // for time()[/blue]
: : : : : #include
: : : : :
: : : : :
: : : : : using namespace std;
: : : : :
: : : : : int main()
: : : : : {
: : : : :
: : : : : /* Seed the random-number generator with current time so that
: : : : : * the numbers will be different every time we run
: : : : : */
: : : : :
: : : : : [blue] srand( (unsigned)time( NULL ) );[/blue]
: : : : :
: : : : : // Display 10 numbers
: : : : : for( int i=0; i<10; i++ )
: : : : : cout << [blue]rand()%100[/blue] << endl;
: : : : : // with '%' operator, you can limit the number of
: : : : : // digits that is generated : %10 -> 0~9, %100 -> 0~99 ...
: : : : :
: : : : : getch(); // pause
: : : : :
: : : : : return 0;
: : : : : }
: : : : : [/code]
: : : : : [hr]
: : : : : ;-)
: : : : :
: : : :
: : : :
: : : : Reading any C/C++ FAQ would also have told him to use srand(). Google is your friend.
: : : :
: : : I was reading my C++ book and it said to use the [code] rand(); [/code] insted of the [code]srand();[/code]
: : :
: :
: :
: : What book are you using?
: :
: : If your book tells you to use rand() [italic]instead of[/italic] srand(), the author doesn't even know the language, and I would sincerely recommend you find another book.
: :
: : srand SEEDS rand. That is, pseudo_random numbers are created by using mathmatical equations on a single or multiple seed(s) to produce a very random series of numbers (well, random-looking, if you don't know the seed). The same seed always makes the same series of numbers.
: :
: : srand() isn't something you use instead of rand(), it MUST be used WITH rand(), and there is no way to avoid this.
: :
: :
: : Hope it helps!
: : Sincerely,
: : Jakykong (Jack Mudge)
: : jack_mudge@hotmail.com
: :
: :
: [blue]
: Just wanted to add: if you do not use srand(), rand() will almost (if not always) return the [b]same[/b] value because it is using the same generated number all the time.
:
: srand() generates another number for rand() to use, hence creates truely random numbers.
:
: srand() and rand() are two different routines that work with each other to produce random numbers.
: [/blue]
:
thank you it has helped alot I am using the book created by D.S. Malik
• : : : : :
: : : : : (some parts are from MSDN)
: : : : : [code]
: : : : : #include // for getch()
: : : : : [blue]#include // for rand()
: : : : : #include // for time()[/blue]
: : : : : #include
: : : : :
: : : : :
: : : : : using namespace std;
: : : : :
: : : : : int main()
: : : : : {
: : : : :
: : : : : /* Seed the random-number generator with current time so that
: : : : : * the numbers will be different every time we run
: : : : : */
: : : : :
: : : : : [blue] srand( (unsigned)time( NULL ) );[/blue]
: : : : :
: : : : : // Display 10 numbers
: : : : : for( int i=0; i<10; i++ )
: : : : : cout << [blue]rand()%100[/blue] << endl;
: : : : : // with '%' operator, you can limit the number of
: : : : : // digits that is generated : %10 -> 0~9, %100 -> 0~99 ...
: : : : :
: : : : : getch(); // pause
: : : : :
: : : : : return 0;
: : : : : }
: : : : : [/code]
: : : : : [hr]
: : : : : ;-)
: : : : :
: : : :
: : : :
: : : : Reading any C/C++ FAQ would also have told him to use srand(). Google is your friend.
: : : :
: : : I was reading my C++ book and it said to use the [code] rand(); [/code] insted of the [code]srand();[/code]
: : :
: :
: :
: : What book are you using?
: :
: : If your book tells you to use rand() [italic]instead of[/italic] srand(), the author doesn't even know the language, and I would sincerely recommend you find another book.
: :
: : srand SEEDS rand. That is, pseudo_random numbers are created by using mathmatical equations on a single or multiple seed(s) to produce a very random series of numbers (well, random-looking, if you don't know the seed). The same seed always makes the same series of numbers.
: :
: : srand() isn't something you use instead of rand(), it MUST be used WITH rand(), and there is no way to avoid this.
: :
: :
: : Hope it helps!
: : Sincerely,
: : Jakykong (Jack Mudge)
: : jack_mudge@hotmail.com
: :
: :
: [blue]
: Just wanted to add: if you do not use srand(), rand() will almost (if not always) return the [b]same[/b] value because it is using the same generated number all the time.
:
: srand() generates another number for rand() to use, hence creates truely random numbers.
:
: srand() and rand() are two different routines that work with each other to produce random numbers.
: [/blue]
:

The thing is, that's not quite how they work. First, it's all pseudo-random numbers: it is indeed possible to predict the next random number, assuming you can predict the seed. However, for all intents and purposes, the numbers are hard enough to predict for most programs to call them random.

But also, rand() is the function that generates the number sequence based on the seed. A seed is a number that is used to generate the sequence. srand() does nothing but set some internal data for rand() to use -- a.k.a. the seed. In fact, srand() stands for [i]seed rand[/i].

Sorry if I'm a bit picky about the definition, but in order to properly use functions you do need to understand exactly what they do.

Probably the simplest way to use random numbers in C would be:

[code]
#include //for rand(), srand()
#include //for time()

int main()
{
int x;

srand( (int )time(0) ); //set the

x = rand() //a random number.
x = rand() //another random number
//and so on
}
[/code]

simply because the time is not likely to be the same every time the program is run (unless, of course, you took special effort to make it so). Thus, the time is a good seed: it's hard to predict (although, notably, not impossible to predict)
Sincerely,
Jakykong (Jack Mudge)
jack_mudge@hotmail.com