Вопрос по c – Макрос RAND_MAX: подписанный или неподписанный?

13

Error: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceededссылка 1, ссылка 2) и Linux man (ссылка на сайт) не добавляйте никакой ясности.

Можно подуматьRAND_MAXError: User Rate Limit Exceededsigned int поскольку это то, чтоrand()Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

The ancient Turbo C++ 1.01: #define RAND_MAX 0x7FFFU The not so ancient C++ Builder 5.5: #define RAND_MAX 0x7FFFU The still alive Open Watcom C/C++ 1.9: #define RAND_MAX 32767U DJGPP (gcc 3.3.4 for DOS): #define RAND_MAX 2147483647 MinGW (gcc 4.6.2 for Windows): #define RAND_MAX 0x7FFF MS Visual Studio 2010 (link): RAND_MAX is defined as the value 0x7fff Tiny C Compiler 0.9.25: #define RAND_MAX 0x7FFF lcc-win32 3.8: #define RAND_MAX 0x7fff Pelles C 6.50: #define RAND_MAX 0x3fffffff OR #define RAND_MAX 0x7fff Digital Mars C/C++ 8.52: #define RAND_MAX 32767

Это делает на первый взгляд безобидный код, подобный приведенному ниже, непереносимым и взрывается из-за подписанного или неподписанного продвижения:

cos(w * t) + (rand() - RAND_MAX / 2) * 0.1 / (RAND_MAX / 2);

rand()Error: User Rate Limit Exceededsigned intError: User Rate Limit ExceededRAND_MAX].

Error: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceededunsigned intError: User Rate Limit Exceededrand() получает повышение вunsigned int тоже.

Error: User Rate Limit Exceeded(rand() - RAND_MAX / 2)Error: User Rate Limit ExceededRAND_MAX-RAND_MAXError: User Rate Limit ExceededUINT_MAX+1-RAND_MAX/2,UINT_MAXError: User Rate Limit ExceededRAND_MAX/2,RAND_MAX-RAND_MAX/2].

Error: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceeded

Error: User Rate Limit ExceededintError: User Rate Limit ExceededsignedError: User Rate Limit Exceededsigned intError: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceededint rand()Error: User Rate Limit Exceeded#defineError: User Rate Limit ExceededunsignedError: User Rate Limit ExceededsignedError: User Rate Limit Exceeded martineau
Error: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceeded Alexey Frunze
Error: User Rate Limit ExceededintegerError: User Rate Limit ExceededintError: User Rate Limit ExceededunsignedError: User Rate Limit ExceededintegerError: User Rate Limit Exceededint. Alexey Frunze
Error: User Rate Limit ExceededStandard CError: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceeded<integer constant expression ≥ 32767>Error: User Rate Limit ExceededThe C Programming LanguageError: User Rate Limit Exceededint rand()Error: User Rate Limit Exceeded0Error: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceeded martineau

Ваш Ответ

2   ответа
3

RAND_MAX(int)

RAND_MAX

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Alexey Frunze
7

rand()int

[0, 1)rand()/(RAND_MAX+1.0)rand()rand48

Error: User Rate Limit ExceededunsignedError: User Rate Limit ExceededunsignedError: User Rate Limit Exceeded
Error: User Rate Limit ExceededRAND_MAXError: User Rate Limit Exceeded

Похожие вопросы