Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-02-2007, 10:43 PM   #1 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Unhappy Help needed in C, rand() function.


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
__________________
--- Console Junkie
aditya.shevade is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 10-02-2007, 11:40 PM   #2 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Help needed in C, rand() function.

There is a difference of libraries in gcc and tc. So u have to google to find what u need. I found this.
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 11-02-2007, 06:18 PM   #3 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: Help needed in C, rand() function.

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.
__________________
--- Console Junkie
aditya.shevade is offline  
Old 11-02-2007, 06:47 PM   #4 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Help needed in C, rand() function.

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

Quote:
#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!
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 12-02-2007, 05:22 PM   #5 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: Help needed in C, rand() function.

Thanks man. Repped ya.
__________________
--- Console Junkie
aditya.shevade is offline  
Old 12-02-2007, 05:27 PM   #6 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Help needed in C, rand() function.

Glad to help!
__________________
Bad Bad server.....No candy for u!
mediator is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by clinton
- by Charan
- by Krow

Advertisement




All times are GMT +5.5. The time now is 11:54 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2