Saturday, May 22, 2010

To use the library function rand in c program?

i want to create a timetable for 5 days and 8 periods with 10 subjects.to select the subjects randomly i used rand function.but this generates the same number more than one time for a day.how could i avoid it????also i want the individual number to be generated for a particular number of times per week.plz help me.......................

To use the library function rand in c program?
The rand() function generates random numbers but this does not mean that it'll generate all different numbers.





Consider the following iteration :





randomize();


for(i=0;i%26lt;10;i+=1)


{


printf("%d",rand()%10);


}





the program will print random numbers under 10, 10 times. This program code does not imply that 8 may not be repeated twice.





Now, coming to your program, each day has 8 periods to which any of the 10 subjects without any repetitions have to be applied.





Make an array of size 8 denoting the periods per day like ( per[8]);





now assign the first period as





per[0]=rand()%10;





now for the subsequent assignments compare with the previous elements of the array. If any of them are equal to the value currently assigned, run the randomize statement again. In this way, you'll be able to assign all distinct values to the array.





hope this helps.
Reply:Which compiler are you using, and for which operating system?


If you're using Borland Turbo C++ 3.0 for DOS, then you can simply view the example code in the IDE's built-in help file.





I don't want to do your homework for you, so I'll just help you with the rand() function.


When creating random numbers, you must first 'seed' the random number generator, otherwise your program will always come up with the same 'random' numbers. In 16 bit compilers, rand() returns a signed 16 bit random number, and in 32 bit compilers, rand() returns a signed 32 bit random number. You can modify the the line that calls rand() in order to modify the output number to suit your needs. Just be careful with the numbers when you use rand(). For example, if you're using a 16 bit compiler, you must remember that rand() can only give a random number up to +32767. But you can go up to +65535 if you use casting to make a variable an unsigned one.





Example:


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;


time_t t;


int num;





int main(void)


{


srand((unsigned) time(%26amp;t)); // seed random number generator





// Random number between +1300 and +1400:


num=(1300+rand()%1400);


// Random number between +200 to + 2000, in multiples of 10:


num=((200+(rand() % 180)*10));


// Random number between +1 to +50:


num=(1+rand() % 50);





return 0;


}


Look in your compiler's help file and information on the Internet for more sample code.
Reply:Well, I know c++ and c are different but the concept is the same. Run the rand function in an if loop for the day, the loop guidelines should say something like





rand=p8





if (rand=p1,p2,p3,p4,p5,p6,p7,)


{rand=p8}





that way the period is changed until the rand compiles a different number. Not sure what the rand code looks like in c though. i mostly used srand which created a number based on an algorithm in the computers date
Reply:The easiest way to ensure you don't get duplicates, is to check each time for duplicates, and get a new random number if one crops up. If you're doing it this way, it may be easier to store an index giving you a quick way of looking up a number which has already been used.





However, if you want to ensure that you never duplicate a day/period, it may be easier to walk through the days and periods and randomly pick the subject (rather than randomly pick all the day, period and subject).





If you want to limit the number of occurrences of a subject, then keep a tally of subjects and get a fresh number if you don't like the one you've been given. Each time you get an acceptable subject, reduce the tally. If the tally reaches 0, the subject isn't acceptable again.





For example:





/* define the maximum number or times we are prepared to loop to find an availiable subject before giving up */


#define NUMBER_OF_ATTEMPTS 10





/* define the number of times each subject may be picked, always make the 0th item more than the number of periods in a week, to be sure there is something that can always be picked. Each number corresponds to the number of times you'll allow a particular subject (ie 8 for subject 1 and 6 for subject 3 */


int subject_tally[]={9999, 8,8,6,6,4};





/* The array containing your final data*/


int picks[5][8];


/* The loop counter variables */


int day, period, subject, attempts;





/* initialise random seed */


srand (time(NULL));





/* loop through each period of each day*/


for (day=0; day%26lt;5; day++)


{


for (period=0; period%26lt;8; period++)


{


attempts=0;


/*attempts loop prevents run away */


while (attempts++%26lt;NUMBER_OF_ATTEMPTS)


{


subject = rand() %6 ; /* gives a random number between 0 and 5 */


/* only consider the subject if it still has an excess of available periods*/


if (subject_tally[subject]%26gt;0)


{


picks[day][period]=subject;


subject_tally[subject]--;


/* Leave this while loop if a subject was successfully picked*/


break;


}


}


/* if we ran out of attempts, then make it a free period */


if (attempts %26gt; NUMBER_OF_ATTEMPTS)


{


picks[day][period]=0;


}


}


}


No comments:

Post a Comment