Random Numbers

W

Wilson

I am new to c++ and part of my program needs to create three random
numbers and then output them. I decided to use the computer clock to
help ouput the numbers but i constantly get the same results. I have
used the header file cstdlib for functions rand and srand. Can you
please advise how to alter the program to create random numbers every
time the program is run (or suggest alternate ways)

thanks
 
I

Ian Collins

Wilson said:
I am new to c++ and part of my program needs to create three random
numbers and then output them. I decided to use the computer clock to
help ouput the numbers but i constantly get the same results. I have
used the header file cstdlib for functions rand and srand. Can you
please advise how to alter the program to create random numbers every
time the program is run (or suggest alternate ways)
What program?

Post the relevant code so we can see what you have and how to improve
it. rand() will give the same sequence for the same seed, so you must be
seeding it incorrectly.
 
T

Tim Slattery

Wilson said:
I am new to c++ and part of my program needs to create three random
numbers and then output them. I decided to use the computer clock to
help ouput the numbers but i constantly get the same results. I have
used the header file cstdlib for functions rand and srand. Can you
please advise how to alter the program to create random numbers every
time the program is run (or suggest alternate ways)

srand((unsigned int)time(NULL));

printf("%d\n", rand());
printf("%d\n", rand());

--
Tim Slattery
(e-mail address removed)
http://members.cox.net/slatteryt
 
J

Jim Langston

Wilson said:
I am new to c++ and part of my program needs to create three random
numbers and then output them. I decided to use the computer clock to
help ouput the numbers but i constantly get the same results. I have
used the header file cstdlib for functions rand and srand. Can you
please advise how to alter the program to create random numbers every
time the program is run (or suggest alternate ways)

thanks

This may work for you. Not sure if time() is os dependant.

srand( time() );

std::cout << rand() << "\n";

I think libraries are
#include <time.h>
#include <stdlib>

You might be able to use <ctime>
not sure.
 
V

Victor Bazarov

Jim said:
This may work for you. Not sure if time() is os dependant.

srand( time() );

'time()' is not OS-dependant, but the essence of its return value type
is implementation-specific. Since 'srand' expects 'unsigned', a cast
is an order:

srand ( (unsigned) time() );

Whether it achieves the expected result is implementation-defined, as
well. For all we know, 'time_t' can be such that casting it to
an 'unsigned int' value will always result in, say, 0, which would
defeat the purpose of using the return value of 'time()' as the seed.
IOW, YMMV, but in most cases it works.
std::cout << rand() << "\n";

I think libraries are
#include <time.h>
#include <stdlib>

it's said:
You might be able to use <ctime>
not sure.

V
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

'time()' is not OS-dependant, but the essence of its return value type
is implementation-specific. Since 'srand' expects 'unsigned', a cast
is an order:

srand ( (unsigned) time() );

Whether it achieves the expected result is implementation-defined, as
well. For all we know, 'time_t' can be such that casting it to
an 'unsigned int' value will always result in, say, 0, which would
defeat the purpose of using the return value of 'time()' as the seed.
IOW, YMMV, but in most cases it works.





Those are "headers", not "libraries". And it's not <stdlib>, it's
<stdlib.h>.

<ctime> and <cstdlib> if you want to be correct.
 
V

Victor Bazarov

Erik said:
<ctime> and <cstdlib> if you want to be correct.

Just so that newbies don't get the wrong impression... It's not
"incorrect" to use the .h form of C headers.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top