How to use rand — C99 version
I am writing a C program and i want to use for
like this :
for(int i=0 ; i < something ; i++ )
so I need to use C99 version in order to initialize the counter of for inside the " ( ) " but C99 version doesn't work with rand()
function .
In fact on the man page of rand()
mentions : " that the function is not part of C99 but part of POSIX "
Any ideas in order to avoid initializing the counter before for
..
//UPDATE
The output of compile is :
warning: implicit declaration of function ‘rand_r’ [-Wimplicit-function-declaration]
Code :
array[i] = rand_r(&seed) % MAX + MIN;
- > array[i] type is char* (i have already dynamically allocated space for it)
Thanks
c for-loop random c99
|
show 9 more comments
I am writing a C program and i want to use for
like this :
for(int i=0 ; i < something ; i++ )
so I need to use C99 version in order to initialize the counter of for inside the " ( ) " but C99 version doesn't work with rand()
function .
In fact on the man page of rand()
mentions : " that the function is not part of C99 but part of POSIX "
Any ideas in order to avoid initializing the counter before for
..
//UPDATE
The output of compile is :
warning: implicit declaration of function ‘rand_r’ [-Wimplicit-function-declaration]
Code :
array[i] = rand_r(&seed) % MAX + MIN;
- > array[i] type is char* (i have already dynamically allocated space for it)
Thanks
c for-loop random c99
rand
should work just fine. Did you#include <stdlib.h>
?
– user2357112
May 14 '15 at 5:48
Also, it's right there in the C99 standard, section 7.20.2.1.
– user2357112
May 14 '15 at 5:49
1
Update your question to show clearly how it doesn't work. Show us the code that falls to compile and the error message.
– Keith Thompson
May 14 '15 at 5:50
I want to declare the counter inside everyfor-loop
because I am using many for-loops inside my program and I don't want to have many different variables every time !
– User1911
May 14 '15 at 5:55
I have updated my first post with example + code !
– User1911
May 14 '15 at 6:00
|
show 9 more comments
I am writing a C program and i want to use for
like this :
for(int i=0 ; i < something ; i++ )
so I need to use C99 version in order to initialize the counter of for inside the " ( ) " but C99 version doesn't work with rand()
function .
In fact on the man page of rand()
mentions : " that the function is not part of C99 but part of POSIX "
Any ideas in order to avoid initializing the counter before for
..
//UPDATE
The output of compile is :
warning: implicit declaration of function ‘rand_r’ [-Wimplicit-function-declaration]
Code :
array[i] = rand_r(&seed) % MAX + MIN;
- > array[i] type is char* (i have already dynamically allocated space for it)
Thanks
c for-loop random c99
I am writing a C program and i want to use for
like this :
for(int i=0 ; i < something ; i++ )
so I need to use C99 version in order to initialize the counter of for inside the " ( ) " but C99 version doesn't work with rand()
function .
In fact on the man page of rand()
mentions : " that the function is not part of C99 but part of POSIX "
Any ideas in order to avoid initializing the counter before for
..
//UPDATE
The output of compile is :
warning: implicit declaration of function ‘rand_r’ [-Wimplicit-function-declaration]
Code :
array[i] = rand_r(&seed) % MAX + MIN;
- > array[i] type is char* (i have already dynamically allocated space for it)
Thanks
c for-loop random c99
c for-loop random c99
edited May 14 '15 at 6:12


paxdiablo
630k16912431665
630k16912431665
asked May 14 '15 at 5:44


User1911User1911
5010
5010
rand
should work just fine. Did you#include <stdlib.h>
?
– user2357112
May 14 '15 at 5:48
Also, it's right there in the C99 standard, section 7.20.2.1.
– user2357112
May 14 '15 at 5:49
1
Update your question to show clearly how it doesn't work. Show us the code that falls to compile and the error message.
– Keith Thompson
May 14 '15 at 5:50
I want to declare the counter inside everyfor-loop
because I am using many for-loops inside my program and I don't want to have many different variables every time !
– User1911
May 14 '15 at 5:55
I have updated my first post with example + code !
– User1911
May 14 '15 at 6:00
|
show 9 more comments
rand
should work just fine. Did you#include <stdlib.h>
?
– user2357112
May 14 '15 at 5:48
Also, it's right there in the C99 standard, section 7.20.2.1.
– user2357112
May 14 '15 at 5:49
1
Update your question to show clearly how it doesn't work. Show us the code that falls to compile and the error message.
– Keith Thompson
May 14 '15 at 5:50
I want to declare the counter inside everyfor-loop
because I am using many for-loops inside my program and I don't want to have many different variables every time !
– User1911
May 14 '15 at 5:55
I have updated my first post with example + code !
– User1911
May 14 '15 at 6:00
rand
should work just fine. Did you #include <stdlib.h>
?– user2357112
May 14 '15 at 5:48
rand
should work just fine. Did you #include <stdlib.h>
?– user2357112
May 14 '15 at 5:48
Also, it's right there in the C99 standard, section 7.20.2.1.
– user2357112
May 14 '15 at 5:49
Also, it's right there in the C99 standard, section 7.20.2.1.
– user2357112
May 14 '15 at 5:49
1
1
Update your question to show clearly how it doesn't work. Show us the code that falls to compile and the error message.
– Keith Thompson
May 14 '15 at 5:50
Update your question to show clearly how it doesn't work. Show us the code that falls to compile and the error message.
– Keith Thompson
May 14 '15 at 5:50
I want to declare the counter inside every
for-loop
because I am using many for-loops inside my program and I don't want to have many different variables every time !– User1911
May 14 '15 at 5:55
I want to declare the counter inside every
for-loop
because I am using many for-loops inside my program and I don't want to have many different variables every time !– User1911
May 14 '15 at 5:55
I have updated my first post with example + code !
– User1911
May 14 '15 at 6:00
I have updated my first post with example + code !
– User1911
May 14 '15 at 6:00
|
show 9 more comments
3 Answers
3
active
oldest
votes
rand()
is very much part of the C99 standard, you just have to make sure you include the correct header file.
#include <stdlib.h>
int main(void) {
return rand() % 10;
}
The stdlib.h
header file is covered in 7.20
of the C99 standard, and rand()
is covered in 7.20.2
.
If your man page is stating it's not part of C99, then it's incorrect. Mine (under Debian Jessie) clearly states:
The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
But, of course, it's the standard itself which is the controlling document.
You may be misreading the man page, or whoever wrote the man page may be confused. The random()
and srandom()
functions are not part of C99 (they're POSIX), but rand()
and srand()
definitely are.
Or you may be using rand_r
, the re-entrant version. That too is POSIX but not C99.
If you want to use gcc
in C99 mode to get the for (int i = 0...
functionality, but still want to be able to call non-C99 functions, compile with -std=gnu99
rather than -std=c99
.
Thanks a lot for pointing this out ! you are right i was reading about random() and srandom() :/
– User1911
May 14 '15 at 6:02
That solved my problem.. the-std=gnu99
Thanks a lot and sorry for any inconvenience I might have caused
– User1911
May 14 '15 at 6:18
add a comment |
I'm afraid you've misunderstood several things.
rand()
, rand_r()
, and random()
are three distinct functions.
rand()
is specified by the ISO C standard, and is declared in <stdlib.h>
. It's included in all editions of the C standard (1990, 1999, and 2011). That means that you can depend on any conforming hosted C implementation to provide it (by definition; any hosted implementation that doesn't implement rand()
is non-conforming). (Conforming freestanding implementations are not required to provide rand()
, or any library functions at all; such implementations are generally for embedded systems.)
rand_r()
is not specified by the ISO C standard; instead, it's specified by the POSIX standard. POSIX defines a number of library functions on top of what ISO C defines. The fact that C99 doesn't specify rand_r()
does not mean that you can't use rand_r()
in a C99 program; it merely means that you can't use it in a portable C99 program. (A 100% portable C program cannot depend on POSIX, since not all implementations support POSIX.) rand_r()
, like rand()
, is declared (if it's declared at all) in <stdlib.h>
. You may need to do something extra to enable rand_r()
and other POSIX-specific functions. For gcc, you can do so by not specifying -pedantic
, or by specifying -std=gnu99
(C99 with GNU extensions), or by adding
#define _POSIX_C_SOURCE 1
to the top of your source file, before the #include <stdlib.h>
.
rand_r()
is similar to rand()
, but it's designed to be thread-safe because it uses a seed controlled by the caller rather than an implicit statically allocated seed.
Note that the POSIX standard says that rand_r()
may be removed from a future POSIX standard, probably because there are much better pseudo-random number generators.
random()
is yet another distinct function. It's like rand()
, but somewhat more sophisticated. Like rand_r()
, random()
is defined by POSIX but not by ISO C. Similar considerations apply if you want to use random()
. For reasons that are not entirely clear to me, you need to use different symbol (_SVID_SOURCE
rather than _POSIX_C_SOURCE
). As for any function, you should read the documentation to find out exactly how to use it.
None of this has anything to do with C99-style for
loops. C90 does not permit loops of the form:
for (int i = 0; i < N; i ++) { /* ... */ }
That feature was added to the language by the 1999 standard (and of cource retained in the 2011 standard). This is core feature of the language, unrelated to the standard library.
To use C99-style for
loops, you need to use a compiler that accepts them, possibly with a command-line option to tell it to use the 1999 standard or later.
To use a POSIX-specific function (or any library function), you need to read the documentation for the particular function and do what it says, which includes #include
ing the header that defines it, possibly defining some symbol to enable it, and possibly adding extra command-line options when invoking the compiler or linker.
add a comment |
Use stdlib
library and add following code .
#include <stdlib.h>
var_name =( rand() % 50);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30230090%2fhow-to-use-rand-c99-version%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
rand()
is very much part of the C99 standard, you just have to make sure you include the correct header file.
#include <stdlib.h>
int main(void) {
return rand() % 10;
}
The stdlib.h
header file is covered in 7.20
of the C99 standard, and rand()
is covered in 7.20.2
.
If your man page is stating it's not part of C99, then it's incorrect. Mine (under Debian Jessie) clearly states:
The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
But, of course, it's the standard itself which is the controlling document.
You may be misreading the man page, or whoever wrote the man page may be confused. The random()
and srandom()
functions are not part of C99 (they're POSIX), but rand()
and srand()
definitely are.
Or you may be using rand_r
, the re-entrant version. That too is POSIX but not C99.
If you want to use gcc
in C99 mode to get the for (int i = 0...
functionality, but still want to be able to call non-C99 functions, compile with -std=gnu99
rather than -std=c99
.
Thanks a lot for pointing this out ! you are right i was reading about random() and srandom() :/
– User1911
May 14 '15 at 6:02
That solved my problem.. the-std=gnu99
Thanks a lot and sorry for any inconvenience I might have caused
– User1911
May 14 '15 at 6:18
add a comment |
rand()
is very much part of the C99 standard, you just have to make sure you include the correct header file.
#include <stdlib.h>
int main(void) {
return rand() % 10;
}
The stdlib.h
header file is covered in 7.20
of the C99 standard, and rand()
is covered in 7.20.2
.
If your man page is stating it's not part of C99, then it's incorrect. Mine (under Debian Jessie) clearly states:
The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
But, of course, it's the standard itself which is the controlling document.
You may be misreading the man page, or whoever wrote the man page may be confused. The random()
and srandom()
functions are not part of C99 (they're POSIX), but rand()
and srand()
definitely are.
Or you may be using rand_r
, the re-entrant version. That too is POSIX but not C99.
If you want to use gcc
in C99 mode to get the for (int i = 0...
functionality, but still want to be able to call non-C99 functions, compile with -std=gnu99
rather than -std=c99
.
Thanks a lot for pointing this out ! you are right i was reading about random() and srandom() :/
– User1911
May 14 '15 at 6:02
That solved my problem.. the-std=gnu99
Thanks a lot and sorry for any inconvenience I might have caused
– User1911
May 14 '15 at 6:18
add a comment |
rand()
is very much part of the C99 standard, you just have to make sure you include the correct header file.
#include <stdlib.h>
int main(void) {
return rand() % 10;
}
The stdlib.h
header file is covered in 7.20
of the C99 standard, and rand()
is covered in 7.20.2
.
If your man page is stating it's not part of C99, then it's incorrect. Mine (under Debian Jessie) clearly states:
The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
But, of course, it's the standard itself which is the controlling document.
You may be misreading the man page, or whoever wrote the man page may be confused. The random()
and srandom()
functions are not part of C99 (they're POSIX), but rand()
and srand()
definitely are.
Or you may be using rand_r
, the re-entrant version. That too is POSIX but not C99.
If you want to use gcc
in C99 mode to get the for (int i = 0...
functionality, but still want to be able to call non-C99 functions, compile with -std=gnu99
rather than -std=c99
.
rand()
is very much part of the C99 standard, you just have to make sure you include the correct header file.
#include <stdlib.h>
int main(void) {
return rand() % 10;
}
The stdlib.h
header file is covered in 7.20
of the C99 standard, and rand()
is covered in 7.20.2
.
If your man page is stating it's not part of C99, then it's incorrect. Mine (under Debian Jessie) clearly states:
The functions rand() and srand() conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
But, of course, it's the standard itself which is the controlling document.
You may be misreading the man page, or whoever wrote the man page may be confused. The random()
and srandom()
functions are not part of C99 (they're POSIX), but rand()
and srand()
definitely are.
Or you may be using rand_r
, the re-entrant version. That too is POSIX but not C99.
If you want to use gcc
in C99 mode to get the for (int i = 0...
functionality, but still want to be able to call non-C99 functions, compile with -std=gnu99
rather than -std=c99
.
edited May 14 '15 at 6:06
answered May 14 '15 at 5:47


paxdiablopaxdiablo
630k16912431665
630k16912431665
Thanks a lot for pointing this out ! you are right i was reading about random() and srandom() :/
– User1911
May 14 '15 at 6:02
That solved my problem.. the-std=gnu99
Thanks a lot and sorry for any inconvenience I might have caused
– User1911
May 14 '15 at 6:18
add a comment |
Thanks a lot for pointing this out ! you are right i was reading about random() and srandom() :/
– User1911
May 14 '15 at 6:02
That solved my problem.. the-std=gnu99
Thanks a lot and sorry for any inconvenience I might have caused
– User1911
May 14 '15 at 6:18
Thanks a lot for pointing this out ! you are right i was reading about random() and srandom() :/
– User1911
May 14 '15 at 6:02
Thanks a lot for pointing this out ! you are right i was reading about random() and srandom() :/
– User1911
May 14 '15 at 6:02
That solved my problem.. the
-std=gnu99
Thanks a lot and sorry for any inconvenience I might have caused– User1911
May 14 '15 at 6:18
That solved my problem.. the
-std=gnu99
Thanks a lot and sorry for any inconvenience I might have caused– User1911
May 14 '15 at 6:18
add a comment |
I'm afraid you've misunderstood several things.
rand()
, rand_r()
, and random()
are three distinct functions.
rand()
is specified by the ISO C standard, and is declared in <stdlib.h>
. It's included in all editions of the C standard (1990, 1999, and 2011). That means that you can depend on any conforming hosted C implementation to provide it (by definition; any hosted implementation that doesn't implement rand()
is non-conforming). (Conforming freestanding implementations are not required to provide rand()
, or any library functions at all; such implementations are generally for embedded systems.)
rand_r()
is not specified by the ISO C standard; instead, it's specified by the POSIX standard. POSIX defines a number of library functions on top of what ISO C defines. The fact that C99 doesn't specify rand_r()
does not mean that you can't use rand_r()
in a C99 program; it merely means that you can't use it in a portable C99 program. (A 100% portable C program cannot depend on POSIX, since not all implementations support POSIX.) rand_r()
, like rand()
, is declared (if it's declared at all) in <stdlib.h>
. You may need to do something extra to enable rand_r()
and other POSIX-specific functions. For gcc, you can do so by not specifying -pedantic
, or by specifying -std=gnu99
(C99 with GNU extensions), or by adding
#define _POSIX_C_SOURCE 1
to the top of your source file, before the #include <stdlib.h>
.
rand_r()
is similar to rand()
, but it's designed to be thread-safe because it uses a seed controlled by the caller rather than an implicit statically allocated seed.
Note that the POSIX standard says that rand_r()
may be removed from a future POSIX standard, probably because there are much better pseudo-random number generators.
random()
is yet another distinct function. It's like rand()
, but somewhat more sophisticated. Like rand_r()
, random()
is defined by POSIX but not by ISO C. Similar considerations apply if you want to use random()
. For reasons that are not entirely clear to me, you need to use different symbol (_SVID_SOURCE
rather than _POSIX_C_SOURCE
). As for any function, you should read the documentation to find out exactly how to use it.
None of this has anything to do with C99-style for
loops. C90 does not permit loops of the form:
for (int i = 0; i < N; i ++) { /* ... */ }
That feature was added to the language by the 1999 standard (and of cource retained in the 2011 standard). This is core feature of the language, unrelated to the standard library.
To use C99-style for
loops, you need to use a compiler that accepts them, possibly with a command-line option to tell it to use the 1999 standard or later.
To use a POSIX-specific function (or any library function), you need to read the documentation for the particular function and do what it says, which includes #include
ing the header that defines it, possibly defining some symbol to enable it, and possibly adding extra command-line options when invoking the compiler or linker.
add a comment |
I'm afraid you've misunderstood several things.
rand()
, rand_r()
, and random()
are three distinct functions.
rand()
is specified by the ISO C standard, and is declared in <stdlib.h>
. It's included in all editions of the C standard (1990, 1999, and 2011). That means that you can depend on any conforming hosted C implementation to provide it (by definition; any hosted implementation that doesn't implement rand()
is non-conforming). (Conforming freestanding implementations are not required to provide rand()
, or any library functions at all; such implementations are generally for embedded systems.)
rand_r()
is not specified by the ISO C standard; instead, it's specified by the POSIX standard. POSIX defines a number of library functions on top of what ISO C defines. The fact that C99 doesn't specify rand_r()
does not mean that you can't use rand_r()
in a C99 program; it merely means that you can't use it in a portable C99 program. (A 100% portable C program cannot depend on POSIX, since not all implementations support POSIX.) rand_r()
, like rand()
, is declared (if it's declared at all) in <stdlib.h>
. You may need to do something extra to enable rand_r()
and other POSIX-specific functions. For gcc, you can do so by not specifying -pedantic
, or by specifying -std=gnu99
(C99 with GNU extensions), or by adding
#define _POSIX_C_SOURCE 1
to the top of your source file, before the #include <stdlib.h>
.
rand_r()
is similar to rand()
, but it's designed to be thread-safe because it uses a seed controlled by the caller rather than an implicit statically allocated seed.
Note that the POSIX standard says that rand_r()
may be removed from a future POSIX standard, probably because there are much better pseudo-random number generators.
random()
is yet another distinct function. It's like rand()
, but somewhat more sophisticated. Like rand_r()
, random()
is defined by POSIX but not by ISO C. Similar considerations apply if you want to use random()
. For reasons that are not entirely clear to me, you need to use different symbol (_SVID_SOURCE
rather than _POSIX_C_SOURCE
). As for any function, you should read the documentation to find out exactly how to use it.
None of this has anything to do with C99-style for
loops. C90 does not permit loops of the form:
for (int i = 0; i < N; i ++) { /* ... */ }
That feature was added to the language by the 1999 standard (and of cource retained in the 2011 standard). This is core feature of the language, unrelated to the standard library.
To use C99-style for
loops, you need to use a compiler that accepts them, possibly with a command-line option to tell it to use the 1999 standard or later.
To use a POSIX-specific function (or any library function), you need to read the documentation for the particular function and do what it says, which includes #include
ing the header that defines it, possibly defining some symbol to enable it, and possibly adding extra command-line options when invoking the compiler or linker.
add a comment |
I'm afraid you've misunderstood several things.
rand()
, rand_r()
, and random()
are three distinct functions.
rand()
is specified by the ISO C standard, and is declared in <stdlib.h>
. It's included in all editions of the C standard (1990, 1999, and 2011). That means that you can depend on any conforming hosted C implementation to provide it (by definition; any hosted implementation that doesn't implement rand()
is non-conforming). (Conforming freestanding implementations are not required to provide rand()
, or any library functions at all; such implementations are generally for embedded systems.)
rand_r()
is not specified by the ISO C standard; instead, it's specified by the POSIX standard. POSIX defines a number of library functions on top of what ISO C defines. The fact that C99 doesn't specify rand_r()
does not mean that you can't use rand_r()
in a C99 program; it merely means that you can't use it in a portable C99 program. (A 100% portable C program cannot depend on POSIX, since not all implementations support POSIX.) rand_r()
, like rand()
, is declared (if it's declared at all) in <stdlib.h>
. You may need to do something extra to enable rand_r()
and other POSIX-specific functions. For gcc, you can do so by not specifying -pedantic
, or by specifying -std=gnu99
(C99 with GNU extensions), or by adding
#define _POSIX_C_SOURCE 1
to the top of your source file, before the #include <stdlib.h>
.
rand_r()
is similar to rand()
, but it's designed to be thread-safe because it uses a seed controlled by the caller rather than an implicit statically allocated seed.
Note that the POSIX standard says that rand_r()
may be removed from a future POSIX standard, probably because there are much better pseudo-random number generators.
random()
is yet another distinct function. It's like rand()
, but somewhat more sophisticated. Like rand_r()
, random()
is defined by POSIX but not by ISO C. Similar considerations apply if you want to use random()
. For reasons that are not entirely clear to me, you need to use different symbol (_SVID_SOURCE
rather than _POSIX_C_SOURCE
). As for any function, you should read the documentation to find out exactly how to use it.
None of this has anything to do with C99-style for
loops. C90 does not permit loops of the form:
for (int i = 0; i < N; i ++) { /* ... */ }
That feature was added to the language by the 1999 standard (and of cource retained in the 2011 standard). This is core feature of the language, unrelated to the standard library.
To use C99-style for
loops, you need to use a compiler that accepts them, possibly with a command-line option to tell it to use the 1999 standard or later.
To use a POSIX-specific function (or any library function), you need to read the documentation for the particular function and do what it says, which includes #include
ing the header that defines it, possibly defining some symbol to enable it, and possibly adding extra command-line options when invoking the compiler or linker.
I'm afraid you've misunderstood several things.
rand()
, rand_r()
, and random()
are three distinct functions.
rand()
is specified by the ISO C standard, and is declared in <stdlib.h>
. It's included in all editions of the C standard (1990, 1999, and 2011). That means that you can depend on any conforming hosted C implementation to provide it (by definition; any hosted implementation that doesn't implement rand()
is non-conforming). (Conforming freestanding implementations are not required to provide rand()
, or any library functions at all; such implementations are generally for embedded systems.)
rand_r()
is not specified by the ISO C standard; instead, it's specified by the POSIX standard. POSIX defines a number of library functions on top of what ISO C defines. The fact that C99 doesn't specify rand_r()
does not mean that you can't use rand_r()
in a C99 program; it merely means that you can't use it in a portable C99 program. (A 100% portable C program cannot depend on POSIX, since not all implementations support POSIX.) rand_r()
, like rand()
, is declared (if it's declared at all) in <stdlib.h>
. You may need to do something extra to enable rand_r()
and other POSIX-specific functions. For gcc, you can do so by not specifying -pedantic
, or by specifying -std=gnu99
(C99 with GNU extensions), or by adding
#define _POSIX_C_SOURCE 1
to the top of your source file, before the #include <stdlib.h>
.
rand_r()
is similar to rand()
, but it's designed to be thread-safe because it uses a seed controlled by the caller rather than an implicit statically allocated seed.
Note that the POSIX standard says that rand_r()
may be removed from a future POSIX standard, probably because there are much better pseudo-random number generators.
random()
is yet another distinct function. It's like rand()
, but somewhat more sophisticated. Like rand_r()
, random()
is defined by POSIX but not by ISO C. Similar considerations apply if you want to use random()
. For reasons that are not entirely clear to me, you need to use different symbol (_SVID_SOURCE
rather than _POSIX_C_SOURCE
). As for any function, you should read the documentation to find out exactly how to use it.
None of this has anything to do with C99-style for
loops. C90 does not permit loops of the form:
for (int i = 0; i < N; i ++) { /* ... */ }
That feature was added to the language by the 1999 standard (and of cource retained in the 2011 standard). This is core feature of the language, unrelated to the standard library.
To use C99-style for
loops, you need to use a compiler that accepts them, possibly with a command-line option to tell it to use the 1999 standard or later.
To use a POSIX-specific function (or any library function), you need to read the documentation for the particular function and do what it says, which includes #include
ing the header that defines it, possibly defining some symbol to enable it, and possibly adding extra command-line options when invoking the compiler or linker.
edited Nov 20 '18 at 2:44
answered May 14 '15 at 16:09
Keith ThompsonKeith Thompson
190k25283472
190k25283472
add a comment |
add a comment |
Use stdlib
library and add following code .
#include <stdlib.h>
var_name =( rand() % 50);
add a comment |
Use stdlib
library and add following code .
#include <stdlib.h>
var_name =( rand() % 50);
add a comment |
Use stdlib
library and add following code .
#include <stdlib.h>
var_name =( rand() % 50);
Use stdlib
library and add following code .
#include <stdlib.h>
var_name =( rand() % 50);
answered May 14 '15 at 5:50


Arshid KVArshid KV
5,41232026
5,41232026
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30230090%2fhow-to-use-rand-c99-version%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
rand
should work just fine. Did you#include <stdlib.h>
?– user2357112
May 14 '15 at 5:48
Also, it's right there in the C99 standard, section 7.20.2.1.
– user2357112
May 14 '15 at 5:49
1
Update your question to show clearly how it doesn't work. Show us the code that falls to compile and the error message.
– Keith Thompson
May 14 '15 at 5:50
I want to declare the counter inside every
for-loop
because I am using many for-loops inside my program and I don't want to have many different variables every time !– User1911
May 14 '15 at 5:55
I have updated my first post with example + code !
– User1911
May 14 '15 at 6:00