Help needed in C, rand() function.

Status
Not open for further replies.

aditya.shevade

Console Junkie
Hi

I am providing a code here which is a bulls cows game. I have compiled it using gcc. The program runs fine, but the rand function always generates 8467 as the first number.

Earlier I had used turboc and compiled the prgram. That program selects a differant value everytime. But with gcc, it's always the same sequence.

I have made one change in the source code while compiling with gcc, and that is, that I have removed the randomize() function. I had written the code as

Code:
input(u)
{
	printf("\n\n\n\n\n\n\t\tPlease Press Enter To Start The Game\n\n\n\t\tIf You Want To Quit The Game Before It Finishes\n\n\n\t\tThen Enter The Number -1.\n\n\n\n\t\t\t\t");
	getch();
	randomize();
	u = rand() % 10000;
	clrscr();

	return (u);
}

in turboc. But since gcc does not recognize randomize I had to remove that line. Is it because of that, that the numbers are always selected in the same sequence?

What should I do to make sure, that the computer selects a differant number everytime?

The source code is :-

Code:
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<stdlib.h>
#include<time.h>

int input(int);
void thankyou(void);
void clrscr(void);

main()
{
	int w,x=0,r1,r2,r3,r4,r5,r6,r7,r8,y,nc=0,pc=0,quit;
	
	clrscr();
    y = input(w);

	r2=(y-y%10)/10;
	r4=(r2-r2%10)/10;
	r6=(r4-r4%10)/10;
	if(y%10==r2%10||y%10==r4%10||y%10==r6%10||r2%10==r4%10||r2%10==r6%10||r4%10==r6%10)
	{
		main();
	}
	if(y<1000||y>10000)
	{
		main();
	}
	if(y>999&&y<10000)
	{
		while(x!=y)
		{

			printf("\n\n\t\tEnter Your Choise Between 999 And 10000\n\n\t\t\t\t\n\n No = %d",y);
			scanf("%d",&x);
			if(x<10000&&x>999)
			{
				pc=0;
				nc=0;
				if(x%10==y%10)
				pc++;
				r1=(x-x%10)/10;
				r2=(y-y%10)/10;
				if(r1%10==r2%10)
				pc++;
				r3=(r1-r1%10)/10;
				r4=(r2-r2%10)/10;
				if(r3%10==r4%10)
				pc++;
				r5=(r3-r3%10)/10;
				r6=(r4-r4%10)/10;
				if(r5%10==r6%10)
				pc++;
				if( y%10==x%10||y%10== r1%10||y%10== r3%10||y%10== r5%10)
				nc++;
				if(r2%10==x%10||r2%10==r1%10||r2%10==r3%10||r2%10==r5%10)
				nc++;
				if(r4%10==x%10||r4%10==r1%10||r4%10==r3%10||r4%10==r5%10)
				nc++;
				if(r6%10==x%10||r6%10==r1%10||r6%10==r3%10||r6%10==r5%10)
				nc++;

				printf("\n\n\t\t%d - Positions Correct",pc);
				printf("\n\n\t\t%d - Numbers Correct",nc);
				if(pc==4&&nc==4)
				{
					printf("\n\n\n\n\n\t\tThe Number You Asked Me To Guess Is \n\n\n\n\n\t\t\t\t%d\n\n\n",x);
					printf ("\n\n\t\tPress 1 to play again or any other key to quit\n\n\n\t\t");
					scanf ("\n\n%d",&quit);
					if (quit==1)
					{
						main();
					}
					else
					{
						thankyou();
					}
				}
			}
			if (x==-1)
			{
				printf("\n\n\n\t\tThank You For Playing The Game. Hope You Enjoyed.");
				getch();
				abort();
			}
			if (x<1000||x>10000)
			printf("\n\t\tThe Number You Have To Guess Is Between 999 And 10000");
		}
	}
	else
	printf("\n\n\n\n\n\tThe Number You Want Me To Guess Should Be Between 999 And 10000\n\n\t\t\t\t");
	getch();
	return 0;
}

int input(int u)
{
	printf("\n\n\n\n\n\n\t\tPlease Press Enter To Start The Game\n\n\n\t\tIf You Want To Quit The Game Before It Finishes\n\n\n\t\tThen Enter The Number -1.\n\n\n\n\t\t\t\t");
	getch();
    u = rand() % 10000;
	return (u);
}

void thankyou(void)
{
	printf ("\n\n\n\t\tThank You For Playing This Game. See You Next Time");
	getch();
	abort();
}

void clrscr(void)
{
     int i;
     
     for (i=0;i<30;i++)
     {
         printf ("\n");
         }
         }

I know that there are some un-necessary lines in the code, but earlier I had created a 2 player game, evetually I converted it to a single player one, but was too lazy to remove unwanted lines. Please help me guys.

Aditya
 

mediator

Technomancer
There is a difference of libraries in gcc and tc. So u have to google to find what u need. I found this.
 
OP
aditya.shevade

aditya.shevade

Console Junkie
Uh..... I did not understand that. And that is why I have posted this post. I think that the randomize function depends on time or something like that, and maybe because of that, each time a differant number is chosen. So what function can I use instead of randomize, or is there any other way to achieve it. You know that those people only know tc in colleges (at least in mine), so no one knows. Please guys.
 

mediator

Technomancer
OK, I tried and here's what I got!

#include <iostream.h>
#include <time.h>
#include<stdlib.h>


int main()
{
srand(time(NULL));
int a=(rand () % 1000) + 1;
cout<<a<<endl;
return 0;
}
This generates random numbers as u want. Change the 2nd bolded part to 100 to get numbers below 100. U may put the effective code into some function and use it as a library!

Hope this works! :)
 
Status
Not open for further replies.
Top Bottom