C programming problem

2006-10-30 11:52 am
Hi, I know that RAND_MAX is defined as the value 0x7fff.

How can i convert the random number from the range [0, RAND_MAX] to [10, 500]??

Please help~~~~
Thanks~~~

回答 (3)

2006-10-30 12:01 pm
✔ 最佳答案
srand( time( 0 ) );
int x;
x = 10 + rand() % 500

咁就可以做到random generate range 10 - 500的integer
參考: my programming knowledge
2006-10-30 7:20 pm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main()
{
int i;

srand(time(NULL));

// rand() % 491[0-490]
// rand() % 491 + 10[10-500]
i = rand() % 491 + 10;

printf(”%d\n”, i);
}

2006-11-01 23:23:30 補充:
咁 rand 一定會出過 500 的數
2006-10-30 12:00 pm
用得出黎ge random number去mod 500, if細過10就唔要


收錄日期: 2021-04-12 22:47:53
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061030000051KK00646

檢視 Wayback Machine 備份