我想将我生成的一个随机数传递给一个int数组。我得到的东西,但它不正确。如何把随机数放在int数组中C
由于提前,
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, n;
time_t t;
n = 5;
int haystack [n];
/* Intializes random number generator */
srand((unsigned) time(&t));
/* Print 5 random numbers from 0 to 50 */
for(i = 0 ; i < n ; i++)
{
printf("%d\n", rand() % 50);
haystack[i] = rand();
printf("%d\n", haystack[i]);
}
return(0);
}
什么是_not right_? – LPs
int haystackp []打印数字,但不是我用srand时间和rand生成的数字,直到数字50为止。它用自己的种子生成自己的数字。 – Bejns
你正在调用rand()两次,你会得到两个不同的随机数。如果您想重复使用随机数,请将其存储在变量中,并在打印和赋值语句中重复使用。 – dubes