# Thread: New to C need some help.

1. ## New to C need some help.

Ok so I'm in school learning C right now, we use Ubuntu for the C and compiling and I got bored the other day so I got ahead of the class and I am working on making a guessing game program, I made on in VB once and it was easy and now I'm trying it in C and I got most of it fine but now I need to make it so it generates a random number as the variable "a"

I have it set right now so a=43 and the program is working now I just need to make a the random number that it generates however after some looking around I'm not sure what to do, I have tried a couple of codes but they haven't worked. Anyways if anyone could help me out with this it would be awesome!

Thanks
~Pink Bandanna Guy~

2. Code:
`a = rand() % 10; //random numbers, from 0 to 9`
If you need to seed the random number generator, look into srand();

3. heres a straightforward tutorial, in the FAQ: Cprogramming.com FAQ > Generate random numbers?

4. You can start off by posting your code.

5. Code:
```#include <stdio.h>
int main(void)

{

int j=1,n,t,a=43,r; //need to make "a" a random number.

printf("Enter a number:");
scanf("%i",&n);

do {
if (n>a){
r = 5-j;
printf("Your guess was to high guess again (only %i more guesses): ",r);
scanf("%i",&n);
j++;
}
if (n<a) {
r = 5-j;
printf("Your guess was to low guess again (only %i more guesses): ",r);
scanf("%i",&n);
j++;
}
if (n==a) {
printf ("CORRECT!\n");
j=15;

}

}
while (j<5);

if (j>=5 && j<=10){
printf ("To many guesses you lose\n\n");
}
//When guessing all numbers to low or all to high it misses 1 count, it ends on 1 guess left.  If it does a "your guess is to low" then a your guess is to high  it ends fine.

return 0;

}```
Yes I know my code sucks but we all begin some where ? anyways thanks for the help Ill try looking into the random code Adak gave and maybe look into seeding, if you guys have any recommendations please post them, I really want to learn this stuff so all constructive criticism is welcome.

6. You should adopt a better coding style. At least, make all the braces line up in the correct columns (i.e. and open should be in the same logical column as a close)
Code:
```int main(void)
{
int i;
for (i = 0; i < 10; i++){
printf("%i\n");
}
return 0;
}```
Something like that (this is K&R style).

But, as far as your logic goes: a quick look and I saw this:
if (n==a) {
printf ("CORRECT!\n");
j=15;

I would suggest you use break; in place of j=15.

7. Originally Posted by Kennedy
You should adopt a better coding style. At least, make all the braces line up in the correct columns (i.e. and open should be in the same logical column as a close)
Code:
```int main(void)
{
int i;
for (i = 0; i < 10; i++){
printf("%i\n");
}
return 0;
}```
Something like that (this is K&R style).

But, as far as your logic goes: a quick look and I saw this:
if (n==a) {
printf ("CORRECT!\n");
j=15;

I would suggest you use break; in place of j=15.
Ok sounds good! completely forgot about breaks >.< we just read that section too. Anyways thanks for the suggestions!