473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Casts for srand? (FAQ 13.17)

"Each time I run my program, I get the same sequence of numbers back
from rand()."
The answer to question 13.17, in the c-faq is:

#include <stdlib.h>
#include <time.h>

srand((unsigned int)time((time_t *)NULL));
Are all those casts in the srand() call really needed?
Is this ok, instead, relying on implicit casts when needed?

#include <stdlib.h>
#include <time.h>

srand(time(NULL));
/* or even
srand(time(0));
*/
PS: According to the standard, does <stdlib.h#define NULL?
Jan 8 '08 #1
2 2123
On Tue, 08 Jan 2008 12:08:44 -0800, Mara Guida wrote:
"Each time I run my program, I get the same sequence of numbers back
from rand()."

The answer to question 13.17, in the c-faq is:

#include <stdlib.h>
#include <time.h>

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

Are all those casts in the srand() call really needed? Is this ok,
instead, relying on implicit casts when needed?
A cast is an explicit conversion. An implicit cast would be an implicit
explicit conversion.

But no, there's nothing wrong with relying on the implicit conversion
from 0 or NULL to time_t *, and from time_t to unsigned int. Depending on
the implementation, the casts may affect some diagnostics, but no bugs
will be introduced or fixed by them.
PS: According to the standard, does <stdlib.h#define NULL?
Yes. NULL is defined by <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>,
<time.h>, <wchar.h>, and <locale.h>. It's harder to include standard
headers and _not_ get NULL defined, than it is to accidentally leave it
undefined.
Jan 8 '08 #2
In article <9d**********************************@r60g2000hsc. googlegroups.com>,
Mara Guida <ma*******@gmail.comwrote:
>"Each time I run my program, I get the same sequence of numbers back
from rand()."

The answer to question 13.17, in the c-faq is:

#include <stdlib.h>
#include <time.h>

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

Are all those casts in the srand() call really needed?
No. They're probably there for historical reasons.

>Is this ok, instead, relying on implicit casts when needed?
There's no such thing as an "implicit cast"; a cast is a source code
construct (and therefore inherently explicit) that forces a
conversion.
That nitpick aside, assuming you meant "implicit conversion", the
answer to your question is yes, it's perfectly valid.

>#include <stdlib.h>
#include <time.h>

srand(time(NULL));
/* or even
srand(time(0));
*/
If you forgot to #include <time.h>, you won't have a prototype in scope
for time(), and these would pass an integer 0 (or possibly a null void *)
instead of a null time_t *. Pre-ANSI C would have required a cast here
to force the right type to be passed, but now that we have prototypes
(and have for almost two decades) the correct solution is to
#include <time.hand let the compiler do the conversion for you.

Casting the return value is slightly less useless, but only slightly;
time_t (which time() returns) is required to be an arithmetic type, and
every arithmetic type can be converted to unsigned without a cast, but
your compiler might warn about some of them and the cast will tell it
to be quiet (which is in general not such a good idea, but is probably
valid here).
dave

Jan 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David Shaw | last post by:
Hi, I'm writing a fun little program to play "Petals Around the Roses" with... I need to seed my random numbers so that they won't be the same every time I run the program, but my compiler won't...
5
by: Andrew | last post by:
Hello, Is there anyway to detect when scanf implicitly casts a value? If I have a scanf call which expects a float and is passed a double I believe that testing the scanf call with...
20
by: Martijn | last post by:
Hi, Those familiar with Windows programming may be familiar with the windowsx.h header. In this header macros exist that use double casts, like so (hope this is readable): #define...
25
by: Merrill & Michele | last post by:
Many of us watched the World Series of Poker this last week and plotted how we were to take over that world. My current problem begins with shuffling the deck. For the apps I've written before,...
3
by: bobrics | last post by:
Hi, I am using srand() and would like to create different random numbers during a SINGLE execution of my program because I want to compare random cases. For now I have a switch statement within a...
11
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a...
61
by: jacob navia | last post by:
Continuing the discussion about casts, I would like to know your opinions about the hairy subject of casts as lvalues, i.e. This will fail under lcc-win32, but MSVC and gcc will accept it. I...
35
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
This topic is a FAQ. But I have read the faq and spent a couple of hours browsing the group archives, and still have a few questions that I hope you can answer. My understanding is that...
20
by: Bill Cunningham | last post by:
I am stumped on this one. I tried two methods and something just doesn't seem right. I'll try my new syle. #include <stdio.h> #include <stdlib.h> main() { srand(2000); /*seed unsigned */...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.