# randomizing

This is a discussion on randomizing within the C Programming forums, part of the General Programming Boards category; hi i wrote a trivia program but i want to improve it by randomizing the questions in it. but how ...

1. ## randomizing

hi i wrote a trivia program but i want to improve it by randomizing the questions in it. but how do i do it? thanks

Code:
```main()
{
int l;
clrscr();
gotoxy(30,7);printf("[1] QUESTIONS\n");
gotoxy(30,9);printf("[2] QUIT\n\n");
gotoxy(30,13);printf("Choose:  ");
scanf("%d",&l);
if(l==1)
easy();
else if(l==2)
exit();
}
easy()
{
int x;
char d[5];
int i=0;
char a[]="a";
char b[]="b";
char c[]="c";
for(x=0;x<=3;x++)
{
clrscr();
gotoxy(10,5);printf("Direction: Choose the letter of the correct answer.");
gotoxy(10,7);printf("1. What two letters are both symbols for 1,000?");
gotoxy(10,9);printf("%s. K and M.",a);
gotoxy(10,11);printf("%s. A and B.",b);
gotoxy(10,13);printf("%s. L and D",c);
if(strcmp(d,a)==0)
{
gotoxy(12,20);printf("Correct!!!\n");
i=i+1;
x=3;
getch();
}
if(strcmp(d,a)!=0)
{
gotoxy(12,20);printf("Wrong!!!\n");
gotoxy(12,21);printf("Life[%d]",x);
getch();
if(x>=3)
{
main();
}
}
}
for(x=1;x<=3;x++)
{
clrscr();
gotoxy(10,7);printf("2. What's short for Binary Digit?");
gotoxy(10,9);printf("%s. BD.",a);
gotoxy(10,11);printf("%s. Bit.",b);
gotoxy(10,13);printf("%s. Digit.",c);
if(strcmp(d,b)==0)

{
gotoxy(12,20);printf("Correct!!!\n");
i=i+1;
x=3;
getch();
}
else
{
gotoxy(12,20);printf("Wrong!!!\n");
gotoxy(12,21);printf("Life[%d]",x);
getch();
if(x>=3)
{
main();
}
}
}
for(x=1;x<=3;x++)
{
clrscr();
gotoxy(10,7);printf("3. What digit did Arab mathematician al-Khwarizmi give to the West\n            around 800 B/B.?");
gotoxy(10,10);printf("%s. Zero",a);
gotoxy(10,12);printf("%s. One",b);
gotoxy(10,14);printf("%s. Two",c);
if(strcmp(d,a)==0)

{
gotoxy(12,20);printf("Correct!!!\n");
i=i+1;
x=3;
getch();
}
else
{
gotoxy(12,20);printf("Wrong!!!\n");
gotoxy(12,21);printf("Life[%d]",x);
getch();
if(x>=3)
{
main();
}
}
}
for(x=1;x<=3;x++)
{
clrscr();
gotoxy(10,7);printf("4. What word describes a number system with a base of two?");
gotoxy(10,9);printf("%s. Decimal",a);
gotoxy(10,11);printf("%s. Octal",b);
gotoxy(10,13);printf("%s. Binary",c);
if(strcmp(d,c)==0)

{
gotoxy(12,20);printf("Correct!!!\n");
i=i+1;
x=3;
getch();
}
else
{
gotoxy(12,20);printf("Wrong!!!\n");
gotoxy(12,21);printf("Life[%d]",x);
getch();
if(x>=3)
{
main();
}
}
}
for(x=1;x<=3;x++)
{
clrscr();
gotoxy(10,7);printf("5. How many equal sides does an icosahedron have?");
gotoxy(10,9);printf("%s. Ten",a);
gotoxy(10,11);printf("%s. Twenty",b);
gotoxy(10,13);printf("%s. Thirty",c);
if(strcmp(d,b)==0)

{
gotoxy(12,20);printf("Correct!!!\n");
i=i+1;
x=3;
getch();
}
else
{
gotoxy(12,20);printf("Wrong!!!\n");
gotoxy(12,21);printf("Life[%d]",x);
getch();
if(x>=3)
{
main();
}
}
}

}```

2. the function prototype should include return type and parameters type:
Code:
`int main(void)`
and do not call main from your code

you may want to look at rand function

3. What Vart said

+

Randomizing is fairly easy.

What you want to do is import the time library.

Code:
`#include <time.h>`
then randomize a seed based on the time.

Code:
` srand(time(NULL));`
This is the seed that will be used in the rand function, but since it changes with time, your program will see more realistically random.

The rand function can be called by:

Code:
` rand () &#37; X`
Where X is the upper limit on the number. So if you had 10, it would generate between 0 and 10, there are different ways to use rand so as to manipulate the output better, look it up.