如题 包含小数 而且希望每次随机的数字都是不一样的 希望用最少的代码实现 求帮忙
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
for(int i=0;i<5;i++)
{
double d = rand()%80/10.0+2;
printf("%.1lf\n",d);
}
}
#include<stdio.h>
#include<stdlib.h>
int main(){
float a[5];
int i;
//1、生产5个2.0~10.0的随机数
//rand()%8:表示生产0-7的随机数
//rand()%10/10.00:表示生产0.0-0.9的随机数
//rand()%10/100.00:表示生产0.00-0.09的随机数
for(i=0;i<5;i++){
a[i]=rand()%8+2.0+rand()%10/10.00+rand()%10/100.00;
}
return 0;
}
使用这个代码即可 :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
float a = 0;
srand((unsigned)time(NULL));
for( int i = 0 ; i < 5 ; i++ ){
a = 2 + ( rand() % (100 - 20) ) / 10.0 ;
printf("%f ",a);
}
}