By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

problem with generating random numbers !

P: n/a
I want to write a school computer billing system, one of the function
is to distribute machine id using rand() for each student log on,
suppose there's 100 machines, when each person log on, the system will
store user's info such as name, student id number, log on time, log
out time into a database file, when the next student log on, the
function loads the database, and compare the random generated machine
id with the existing ids store in the database, I'm using a while-loop
to generate random ids until the generated one differs from all the
existing ones, or the loop ends when no machine id's available. It
seems there's something wrong with my program, the generated random
ids always overlap the existing ones, hope someone will help me out
with this, below is my code:

#define DATABASE "database.dat"
#define GENRAND(n) (rand()%n)
#define UNOCCUPIED -1 // test whether the machine occupied

typedef struct tagRECORD { // the management record structure
char name[8]; // student name
char studentID[9]; // student ID
char logonTime[25]; // log on time
char logoutTime[25]; // log out time
int machineID; // machine ID
clock_t logon; // log on clock
clock_t logout; // log out clock
}RECORD;

const unsigned totalMachine=100; // total machines

int statistics[totalMachine];

void errorMessage(char *msg) { // show error messages
fprintf(stderr,"error: %s\n",msg);
exit(1);
}

int assignID(void) { // distribute the available machine ID randomly
int i,r;
int id=0;
FILE *fp;
RECORD record;

fp=fopen(DATABASE,"rb");
if(fp==NULL) {
errorMessage("assignID()");
}

for(i=0;!feof(fp) && i<totalMachine;++i) {
fread(&record,sizeof(RECORD),1,fp);
statistics[i]=record.machineID;
}

for(r=0;i<i && statistics[i]!=-1;++r) {
id=GENRAND(totalMachine);
while(statistics[r]==id)
id=GENRAND(totalMachine);
}

fclose(fp);

return id;
}

when initialization in main() I assigned -1 to all the elements of
array statistics[], using the value -1 to test whether the machine is
occupied.

for(i=0;i<totalMachine;++i)
statistics[i]=UNOCCUPIED; // initialize the array
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ru****@sohu.com (sugaray) wrote:

<SNIP>

for(r=0;i<i && statistics[i]!=-1;++r) {

^^^
This loop will never execute.

<SNIP>

Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #2

P: n/a
sugaray wrote:
seems there's something wrong with my program, the generated random
ids always overlap the existing ones, hope someone will help me out
with this, below is my code:


That behavior immediately suggests to me that you're not seeding the
rng with a new value between runs. rand() repeats the same sequence
unless it's seeded with a different number. Try this:

#include <time.h>
....
srand((unsigned) time(NULL));

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.