Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

random() et srandom()

13 réponses
Avatar
dante
Bonjour,

Je suis dans un débat concernant ces deux fonctions.

Je dis que ces deux fonctions ne sont pas ANSI/ISO, mais qu'elles sont
juste dans la libc (qui n'est pas le standard).
mes sources :


http://www.dinkumware.com/manuals/

Dans le stdlib.h il y a :
http://www.dinkumware.com/manuals/?manual=compleat&page=stdlib.html

on y voit rand et srand, et non de random ou srandom

une autre source :
http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html

Sur le site de Libc :
à cette page :
http://www.gnu.org/software/libc/manual/html_node/ISO-C.html#ISO-C

"1.2.1 ISO C

The GNU C library is compatible with the C standard adopted by the
American National Standards Institute (ANSI): American National Standard
X3.159-1989—“ANSI C” and later by the International Standardization
Organization (ISO): ISO/IEC 9899:1990, “Programming languages—C”. We
here refer to the standard as ISO C since this is the more general
standard in respect of ratification. The header files and library
facilities that make up the GNU library are a superset of those
specified by the ISO C standard.

If you are concerned about strict adherence to the ISO C standard, you
should use the `-ansi' option when you compile your programs with the
GNU C compiler. This tells the compiler to define only ISO standard
features from the library header files, unless you explicitly ask for
additional features. See Feature Test Macros, for information on how to
do this.

Being able to restrict the library to include only ISO C features is
important because ISO C puts limitations on what names can be defined by
the library implementation, and the GNU extensions don't fit these
limitations. See Reserved Names, for more information about these
restrictions.

This manual does not attempt to give you complete details on the
differences between ISO C and older dialects. It gives advice on how to
write programs to work portably under multiple C dialects, but does not
aim for completeness. "

Pour faire simple la libc, respecte l'ISO mais y ajoute d'autre
fonctions. c'est bien ça ?

Une autre source :
http://wwwwbs.cs.tu-berlin.de/user-taipan/kraxel/gnuinfo/libc/BSD_Random.html

http://wwwwbs.cs.tu-berlin.de/user-taipan/kraxel/gnuinfo/libc/ANSI_Random.html

http://wwwwbs.cs.tu-berlin.de/user-taipan/kraxel/gnuinfo/libc/Pseudo-Random_Numbers.html


Je pense donc que ces fonctions ne sont pas ANSI, mais BSD (cela ce dit ?)
cependant.
En compilant ce programme :

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("%ld\n",random());
return 0;
}


avec Codde::Blocks (minGW/gcc)

ça me donne une erreur, fonction random non définie

de même avec lcc

(sous windows)

Mais avec GCC sur cygwin (emulateur unix pour windows) ou un unix, cela
fonctionne.

même avec -ansi -pedantic -Wall et autres.


Alors, Ansi ou non ?
Pourquoi cela compile sur unix et non sur windows (API Posix ?) ?

Merci

10 réponses

1 2
Avatar
Pierre Maurette
Bonjour,

Je suis dans un débat concernant ces deux fonctions.

Je dis que ces deux fonctions ne sont pas ANSI/ISO, mais qu'elles sont juste
dans la libc (qui n'est pas le standard).


[...]

Alors, Ansi ou non ?


Non. Pas dans l'index de la norme.

Pourquoi cela compile sur unix et non sur windows (API Posix ?) ?


J'imagine que c'est parce que random() et srandom() ne sont pas posix ?

Je crois que random() et srandom() sont "extérieurement" remplaçables
par les fonctions standard rand() et srand() mais utilisent un algo
plus aléatoire ou avec une plus longue séquence.
Si le générateur rand() et srand() vous suffit, pour ne pas modifier
votre code, vous pourriez faire une macro, ou un truc comme:

inline int random ()
{
return rand();
}

inline void srand(int seed)
{
srand(seed);
}

Bien voir qu'un lecteur de votre code pensera au moins dans un premier
temps appeler random() et srandom(). Je ne sais pas évaluer les
différences de qualité des générateurs, et donc si c'est grave. Ça ne
me paraît pas très bon...

--
Pierre Maurette

Avatar
Eric Levenez
Le 29/07/06 20:03, dans <44cba309$0$29875$, « dante »
<"moiantispam ? libre fr"> a écrit :

Je dis que ces deux fonctions ne sont pas ANSI/ISO, mais qu'elles sont
juste dans la libc (qui n'est pas le standard).


Ces 2 fonctions sont Posix IEEE Std 1003.1-2003 et donc conforment à
l'ISO/IEC 9945:2003 (Base). Par contre elles ne sont pas conforment à
l'ISO/IEC 9899 ("Programming Languages - C").

La "libc" n'existe pas dans le monde Unix surtout quand on parle de la glibc
qui est une bibliothèque du GNU. Les bibliothèques du monde unix n'ont pas
forcément une "libc" ou une "glibc". Les fonctions random et srandom font
partie de ce que l'on appelle la blibliothèque standard ("standard
library").

<http://www.unix.org/single_unix_specification/>

--
Éric Lévénez -- <http://www.levenez.com/>
Unix is not only an OS, it's a way of life.

Avatar
Harpo
Eric Levenez wrote:

Ces 2 fonctions sont Posix IEEE Std 1003.1-2003 et donc conforment à
l'ISO/IEC 9945:2003 (Base). Par contre elles ne sont pas conforment à
l'ISO/IEC 9899 ("Programming Languages - C").


C'est très compliqué.

--
http://patrick.davalan.free.fr/

Avatar
Emmanuel Delahaye
Bonjour,

Je suis dans un débat concernant ces deux fonctions.

Je dis que ces deux fonctions ne sont pas ANSI/ISO,


Exact.

http://mapage.noos.fr/emdel/clib/ed/inc/random.h

--
A+

Emmanuel Delahaye

Avatar
Emmanuel Delahaye
Eric Levenez wrote:

Ces 2 fonctions sont Posix IEEE Std 1003.1-2003 et donc conforment à
l'ISO/IEC 9945:2003 (Base). Par contre elles ne sont pas conforment à
l'ISO/IEC 9899 ("Programming Languages - C").


C'est très compliqué.

"Ces fonctions sont POSIX.1 et non standard C".


C'est plus clair comme ça ?

--
A+

Emmanuel Delahaye


Avatar
dante
Bonjour,

Je suis dans un débat concernant ces deux fonctions.

Je dis que ces deux fonctions ne sont pas ANSI/ISO,


Exact.

http://mapage.noos.fr/emdel/clib/ed/inc/random.h

Merci à tous, pour vos explications.


#ifndef random
#define random(num) (int)(rand()/(double)RAND_MAX * (num))
#endif

le random definit ici, et identique à celui qui est dans l'API Posix.1 ?
Ou c'est une adaptation pour un meilleur aléa comme indiqué dans la FAQ
et Numerical Recipes ?


Avatar
Emmanuel Delahaye
#ifndef random
#define random(num) (int)(rand()/(double)RAND_MAX * (num))
#endif

le random definit ici, et identique à celui qui est dans l'API Posix.1 ?
Ou c'est une adaptation pour un meilleur aléa comme indiqué dans la FAQ
et Numerical Recipes ?
C'est celui de Borland C. Il ressemble à celui de la FAQ. Je n'en sais

pas plus.

--
A+

Emmanuel Delahaye

Avatar
Stéphane Zuckerman
On Sun, 30 Jul 2006, Emmanuel Delahaye wrote:

#ifndef random
#define random(num) (int)(rand()/(double)RAND_MAX * (num))
#endif

le random definit ici, et identique à celui qui est dans l'API Posix.1 ? Ou
c'est une adaptation pour un meilleur aléa comme indiqué dans la FAQ et
Numerical Recipes ?
C'est celui de Borland C. Il ressemble à celui de la FAQ. Je n'en sais pas

plus.



Le random() POSIX est normalement plus "aléatoire" que le rand() du
langage C.

--
"Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
que je veux !"
"The obvious mathematical breakthrough would be development of an easy
way to factor large prime numbers." (Bill Gates, The Road Ahead)


Avatar
Eric Levenez
Le 30/07/06 9:10, dans <44cc5b76$0$29877$, « dante »
<"moiantispam ? libre fr"> a écrit :


#ifndef random
#define random(num) (int)(rand()/(double)RAND_MAX * (num))
#endif

le random definit ici, et identique à celui qui est dans l'API Posix.1 ?


Non, pas du tout. Le prototype de random est :

long random(void);

De plus random est une fonction, pas une macro, on ne peut donc tester sa
présence par un ifdef.

"The random() function shall use a non-linear additive feedback
random-number generator employing a default state array size of 31 long
integers to return successive pseudo-random numbers in the range from 0 to
2^31-1. The period of this random-number generator is approximately 16 x
(2^31-1). The size of the state array determines the period of the
random-number generator. Increasing the state array size shall increase the
period. With 256 bytes of state information, the period of the random-number
generator shall be greater than 2^69."


--
Éric Lévénez -- <http://www.levenez.com/>
Unix is not only an OS, it's a way of life.

Avatar
Emmanuel Delahaye
Le random() POSIX est normalement plus "aléatoire" que le rand() du
langage C.


Je ne vois pas étant donné que l'implémentation du 'rand() du C' n'est
pas définie... Certes, 1 est > 0...

--
A+

Emmanuel Delahaye

1 2