I am planning to create a simple bulls and cows( also know as mastermind board game) in C.The computer is supposed to make 4 digit code in which the allowed digits are 1,2,3,4,5,and 6 ! 0,7,8,9 are to omitted !! How do I make the computer create such a 4 digit number? I know about rand() function !! But how do I make it omit those 4 digits?

rand() can give you a specific range of random numbers. Just set it up for 1 to 5, four times.

1
2
3
5

Inside a loop, you can put the "number" together, from those digits if you want:
5 * 10^0 +
3 * 10^1 +
2 * 10^2 +

etc. You see the pattern.

Post up your function, and we can be a lot more specific with the help.

Using modulo you can generate a number up to pretty much anything:

int number = rand();

generates a number between 0 and the max your system can handle (likely, 2^32 / 2).

int number = rand() % 6;

generates a number between 0 and 5 (as 6/6 = 1 remainder 0). Extending that:

int number = rand() % 6 + 1;

generates a number between 1 and 6.

Also, don't forget to seed your rand otherwise you'll almost certainly end up with the same values every time!

/facepalm missed the 4 digit part.
I guess you could then slap it all together:

int number;
number += (rand() % 6 + 1) * 1000;
number += (rand() % 6 + 1) * 100;
number += (rand() % 6 + 1) * 10;
number += (rand() % 6 + 1);

Edited 6 Years Ago by Auraomega: Misread OP

What exactly do you mean by seeding rand? How do I perform that? I have read somewhere that you can seed rand by time ! But I dont understand how that works or how to implement that ! ! Please help me

rand() will give you the SAME sequence of random numbers every time (it's used that way for testing programs and equipt.).

srand() will give rand() a new starting place or "seed"

/* resets the random number function, 
based on system time */

time_t t;
srand(time(&t));

All these random generators like this, are really not so random ("pseudo random"), unless handled just right - so don't do any serious stuff and count on it being truly 100% random, OK?

is seed is a starting place? What sort of starting place? Please help me understand the exact working of seeding using system time ? is it based on the second of program execution or something of that sort? After loads of googling I found this step

srand ( time(NULL) );

is that same as the code given by adak ? I know a function called RANDOM which will work with only Turbo C++ .. But I am working with Dev C++ .. is that one totally random or pseudo random ? please please help me understand the process of seeding using time !! !!

#include<stdio.h>   
#include<stdlib.h>
#include<time.h>
int main()
{
	
int number=0;
  srand ( time(NULL) );

number+=(rand()%6 + 1) * 1000;
number+=(rand()%6 + 1) * 100;
number+=(rand()%6 + 1) * 10;
number+=(rand()%6 + 1);
printf("%d",number);
getch();
return 0;
}

There is no such thing as random with a PC, everything it does is triggered by something else (the only possible exception is at power on before everything is initialised as the voltages may be 'floating').

A pseudo-random function like rand() simply takes a number and feeds it into a complex algorithm, the output is then used as the input the next time rand() is called. srand() changes the seed, A.K.A. the input number. A basic example could be:

int number = 0;

int pseudorand()
{
	number += 25;
	if(number % 2)
	{
		number /= 2;
	}
	else
	{
		number++;
		number /= 2;
	}
	
	return number;
}

(obviously wouldn't be a good algorithm, but shows the principle).

srand is simply:

void pseudosrand(int in)
{
	number = in;
}

which just changes the value, AKA the seed.

Hopefully you understand my dire explanation D:

>Please help me understand the exact working of seeding using system time?
With any luck (and a good CMOS battery), the time shouldn't ever be the same twice, giving for most applications a suitable starting point for the rand function.

>is that same as the code given by adak?
More or less.

Also are you using Bloodshed Dev-C++ out of interest? I don't know if it's been updated recently but from what I remember it wasn't a fantastic IDE; Code::Blocks would be my suggestion for an IDE as I believe it still uses Mingw (the same compiler that Dev-C++ uses) but is significantly less buggy and more up-to-date.

Thanks a million !! I understood most of it.. Still has one doubt ! the exact working of

srand ( time(NULL) );

what is the null ? Time at the running of the program? So you gotta seed rand only once ,right? at the beginning?


And I was taught C++ using turbo C++V.30 at school .. I hate its interface , and it wont get installed on my Windows 7 64 bit.. So , I had no other option than Dev C++ and Cfree a portable compiler .. Thanks again for suggesting an uptodate compiler ! Will start using it straightaway :)

>Still has one doubt !

Get current time
Get the current calendar time as a time_t object.

The function returns this value, and if the argument is not a null pointer, the value is also set to the object pointed by timer.

Parameters
timer
Pointer to an object of type time_t, where the time value is stored.
Alternativelly, this parameter can be a null pointer, in which case the parameter is not used, but a time_t object is still returned by the function.


Return Value
The current calendar time as a time_t object.

If the argument is not a null pointer, the return value is the same as the one stored in the location pointed by the argument.

If the function could not retrieve the calendar time, it returns a -1 value.

Basically, if you give time a parameter of NULL, it'll instead return the time, if you choose to pass a time_t variable (time_t is just an unsigned int) then time_t should be given the time instead.

>So you gotta seed rand only once ,right? at the beginning?
Yep.

>Thanks again for suggesting an uptodate compiler !
I personally use 3 compilers depending on the aim; Visual-C++ IDE from Microsoft, not my preferred method but it makes compiling games easier. If I'm not doing a game then I usually like my code to be as portable as possible and use Geany for programming and Mingw on Windows or GCC on Linux to compile my applications, it can become a bit much if you're just learning C though to also have to learn how to compile, create a makefile, etc, but there are occasions where this is the only option and having the knowledge makes things easier.

What I'm trying to say I guess is it's personal preference, you just have to find something that works for you... just avoid buggy IDEs ;)

This article has been dead for over six months. Start a new discussion instead.