C的随机数用法

rand()

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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    for (int i = 0; i < 10; i++)
    {
        printf("%d
",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
",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 ",rand()%100+21);
}
system("pause");
return EXIT_SUCCESS;
}

原文地址:https://www.cnblogs.com/saintdingspage/p/10337975.html