473,386 Members | 1,827 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,386 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 2491
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());...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.