瀏覽代碼

Warning on GetRandomValue range limit (#2800)

Added a comment explaining the range limitations of GetRandomValue.
Added a run-time warning TRACELOG when GetRandomValue is called with an invalid range.
pull/2804/head
Pere001 2 年之前
committed by GitHub
父節點
當前提交
c8fd93d356
沒有發現已知的金鑰在資料庫的簽署中 GPG 金鑰 ID: 4AEE18F83AFDEB23
共有 1 個檔案被更改,包括 7 行新增0 行删除
  1. +7
    -0
      src/rcore.c

+ 7
- 0
src/rcore.c 查看文件

@ -2816,6 +2816,13 @@ int GetRandomValue(int min, int max)
max = min;
min = tmp;
}
// WARNING: Ranges higher than RAND_MAX will return invalid results. More specifically, if (max - min) > INT_MAX there will
// be an overflow, and otherwise if (max - min) > RAND_MAX the random value will incorrectly never exceed a certain threshold.
if ((unsigned int)(max - min) > (unsigned int)RAND_MAX)
{
TRACELOG(LOG_WARNING, "Invalid GetRandomValue arguments. Range should not be higher than %i.", RAND_MAX);
}
return (rand()%(abs(max - min) + 1) + min);
}

Loading…
取消
儲存