473,287 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

problem with generating random numbers !

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
2 2484
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack Higgs | last post by:
I'm building a maths program for year 6 children. Problem with division - I generate a random number between say 1 and 1000. I want to generate another random number which when divided by the...
5
by: Ross MacGregor | last post by:
I have a very simple yet complicated problem. I want to generate a random list of indices (int's) for a container. Let's say I have a container with 10 items and I want a list of 3 random...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
2
by: jerryau | last post by:
Hi, I'm creating a number generator program, that is supposed to generate 6 unique random numbers for each game. I want to generate this for 6 games. The problem is, that it works for the...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
4
by: Mirek | last post by:
Hi A modulo operation is a common advise for generating random numbers within limited range: random()%r I've profiled my program and found that % consumes about 30% cpu time. Is there an...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.