Freshman C language array, random number

CSDN Q & A 2021-12-30 05:37:28 阅读数:150

freshman language array random number

Using a random number generator function ,int rand() Generate a random integer at a time , convert to 1-10 The integer , After consecutive times , get 1-10 Non repeating random integer column ,




Refer to the answer 1:

as follows :

img

Code :

#include <stdlib.h>#include <stdio.h>#include <time.h>int main(){ int a[10],i=0,j,t; srand((unsigned int)time(NULL)); while(i<10) { while(1) { t= rand()%10+1; for(j=0;j<i;j++) { if(a[j]== t) break; } if(j==i) { a[i++] = t; break; } } } for(i=0;i<10;i++) printf("%d ",a[i]); return 0;}



Refer to the answer 2:



Refer to the answer 3:
#include <stdio.h>#include <stdlib.h>#include <time.h>int main() { int a[10]; int flag,i =0,t,j; // Use for Cycle generation 10 A random number  while(i<10){ srand((unsigned)time(NULL)); t = rand()%10;flag=0;for(j=0;j<i;j++){if(a[j] ==t){flag =1;}}if(flag==0){a[i]=t;i++;} } for (i = 0; i < 10; i++) {printf("%d ", a[i]);} return 0;}



Refer to the answer 4:
版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://primo.wiki/2021/12/202112300537268905.html