C的随机数用法

rand()

#include <stdio.h>
#include <stdlib.h>
int main()
{
    for (int i = 0; i < 10; i++)
    {
        printf("%d\n",rand());
    }
    system("pause");
    return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    for (int i = 0; i < 10; i++)
    {
        printf("%d\n",rand()%10);
    }
    system("pause");
    return EXIT_SUCCESS;
}

 指定随机数的生成范围例如生成8以内的随机数---只不过....程序每次运行时都是生成这几个货

#include <stdio.h>
#include <stdlib.h>
int main()
{
    srand(8);
    for (int i = 0; i < 10; i++)
    {
        printf("%d\n",rand()%10);
    }
    system("pause");
    return EXIT_SUCCESS;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
for (int i = 0; i < 10; i++)
{
printf("%d\n",rand()%100+21);
}
system("pause");
return EXIT_SUCCESS;
}

 

posted @ 2019-01-30 14:29  saintdingtheGreat  阅读(154)  评论(0)    收藏  举报