全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:7964
推到 Plurk!
推到 Facebook!

random和rand

尚未結案
xyz
一般會員


發表:9
回覆:4
積分:2
註冊:2003-09-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-30 09:06:45 IP:140.114.xxx.xxx 未訂閱
請問random和rand有何不同在c++使用時需#include<?> 下面這個程式不知如何debug知道的人請告訴我,謝謝. #include #include #include void main() { int a; clrscr(); for(a=1;a<=20;a ) { printf("\n%d",random(6)); } }
gemi0305
版主


發表:81
回覆:564
積分:629
註冊:2003-05-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-09-30 09:28:16 IP:210.66.xxx.xxx 未訂閱
引言: 請問random和rand有何不同在c 使用時需#include<?> 下面這個程式不知如何debug知道的人請告訴我,謝謝. #include #include #include void main() { int a; clrscr(); for(a=1;a<=20;a ) { printf("\n%d",random(6)); } }
這兩者都是include stdlib.h ,所以你如果是在bcb的環境下,就不用特別include了, 除了console環境之外, 這兩個的公用都是一樣的,只不過random是產生0~n-1的亂數,是rand隨機產生一個很大的亂數 請看他們的範例, random的example
 
#include 
#include 
#include     /* prints a random number in the range 0 to 99 */
int main(void)
 {
    randomize();
    printf("Random number in the 0-99 range: %d\n", random (100));
    return 0;
}
rand的example
#include 
#include     int main(void)
{
    int i;        randomize();
    printf("Ten random numbers from 0 to 99\n\n");
    for(i=0; i<10; i  )
       printf("%d\n", rand() % 100);
    return 0;
}     
這裡還有產生小數亂數的文章: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=35815 你的程式,我用看的是沒發現什麼錯,不過建議你還是多多參考範例, 希望對你所幫助 國泰平安
xyz
一般會員


發表:9
回覆:4
積分:2
註冊:2003-09-19

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-09-30 10:04:21 IP:140.114.xxx.xxx 未訂閱
引言:
引言: 請問random和rand有何不同在c 使用時需#include<?> 下面這個程式不知如何debug知道的人請告訴我,謝謝. #include #include #include void main() { int a; clrscr(); for(a=1;a<=20;a ) { printf("\n%d",random(6)); } }
這兩者都是include stdlib.h ,所以你如果是在bcb的環境下,就不用特別include了, 除了console環境之外, 這兩個的公用都是一樣的,只不過random是產生0~n-1的亂數,是rand隨機產生一個很大的亂數 請看他們的範例, random的example
 
#include 
#include 
#include     /* prints a random number in the range 0 to 99 */
int main(void)
 {
    randomize();
    printf("Random number in the 0-99 range: %d\n", random (100));
    return 0;
}
rand的example
#include 
#include     int main(void)
{
    int i;        randomize();
    printf("Ten random numbers from 0 to 99\n\n");
    for(i=0; i<10; i  )
       printf("%d\n", rand() % 100);
    return 0;
}     
這裡還有產生小數亂數的文章: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=35815 你的程式,我用看的是沒發現什麼錯,不過建議你還是多多參考範例, 希望對你所幫助 國泰平安 < face="Verdana, Arial, Helvetica"> 我在visual c run上面的程式會有下面的錯誤,不知該如何解決 c:\program files\common files\system\mapi\1028\nt\1289798.cpp(8) : error C2065: 'randomize' : undeclared identifier c:\program files\common files\system\mapi\1028\nt\1289798.cpp(9) : error C2065: 'random' : undeclared identifier
gemi0305
版主


發表:81
回覆:564
積分:629
註冊:2003-05-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-09-30 10:17:42 IP:210.66.xxx.xxx 未訂閱
在VC上的話,你就用rand() 就好,必畢bcb和vc是有點不同的, 而randomize在VC上,是用另一個srand ()替代, 請看說明:     
Use the srand function to seed the pseudorandom-number generator before calling rand.    Example    /* RAND.C: This program seeds the random-number generator
 * with the time, then displays 10 random integers.
 */    #include 
#include 
#include     void main( void )
{
   int i;       /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );       /* Display 10 numbers. */
   for( i = 0;   i < 10;i   )
      printf( "  m\n", rand() );
}     
國泰平安
xyz
一般會員


發表:9
回覆:4
積分:2
註冊:2003-09-19

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-09-30 11:04:43 IP:140.114.xxx.xxx 未訂閱
引言: 在VC上的話,你就用rand() 就好,必畢bcb和vc是有點不同的, 而randomize在VC上,是用另一個srand ()替代, 請看說明:
Use the srand function to seed the pseudorandom-number generator before calling rand.    Example    /* RAND.C: This program seeds the random-number generator
 * with the time, then displays 10 random integers.
 */    #include 
#include 
#include     void main( void )
{
   int i;       /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );       /* Display 10 numbers. */
   for( i = 0;   i < 10;i   )
      printf( "  m\n", rand() );
}     
國泰平安
請問在VC裡srand就是等於seed?
gemi0305
版主


發表:81
回覆:564
積分:629
註冊:2003-05-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-09-30 12:02:22 IP:210.66.xxx.xxx 未訂閱
seed就是亂數種子,當你用srand就可以取特定的亂數表(種子), 才不會每次都用一樣的亂數表,是為了讓你的隨機亂數更隨機 seed本身不是一個function,是一個隨機亂數中的名詞。 國泰平安
系統時間:2024-05-18 6:03:46
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!