Archive for 十一月 14th, 2012

20121114Prog

隨機亂數rand()的使用

隨機亂數rand()的使用

選不重覆的幾個數字,是從一缸有編號的球中選出幾個球

選不重覆的幾個數字,是從一缸有編號的球中選出幾個球

陣列(array)變數的宣告及變數參數的傳遞。

陣列(array)變數的宣告及變數參數的傳遞。

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

#include 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int i, a;
    srand( (unsigned) time( NULL ) );
    for (i=0; i<6; i++)
    {
 
        a = rand()%49 + 1;
        printf("%d ", a);
    }
    //printf("%d ", RAND_MAX);
    return 0;
}
 
void numgen(int num[6])
int main()
{
    int i;
    int a[6];
 
    srand( (unsigned) time( NULL ) );
    numgen(a);
 
    for (i=0; i<6; i)
    {
        printf("%d ", a[i]);
    }
    return 0;
}
 
void numgen(int num[6])
{
     //int i, j;
     int min;
     int select;
     int balls[49];
     for (i=0; i< 49; i)
     {
         balls[i]=rand();
         //printf("%d ", balls[i]);
     }
     //printf("\n");
     for (j=0; j<6; j)
     {
         select = -1;
         min = RAND_MAX + 1;
         for (i=0; i< 49; i)
         {
             if (balls[i] < min)
             {
                min    = balls[i];
                select = i;        
             }
         }
         num[j] = select + 1;
         balls select  = RAND_MAX + 1
     }
 
}
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,