int 배열에 생성하는 난수를 전달하고 싶습니다. 나는 무언가를 얻지 만 그게 옳지 않다. printf("%d\n", rand() % 50);
난수 배열을 int 배열 C에 넣는 방법
이없는 등의 제한으로 새로운 난수를 생성 : haystack[i] = rand();
그들이 할 할 이유가 없다이 임의의 숫자 < (50) 인쇄
#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 time과 rand로 숫자 50까지 생성 한 숫자가 아닌 숫자를 출력합니다. 자신의 시드로 자신을 생성합니다. – Bejns
rand()를 두 번 호출하면 두 개의 다른 난수가 생깁니다. 난수를 재사용하려면 변수에 저장하고 인쇄 및 할당 문에 다시 사용하십시오. – dubes