EBIWARI
EBIWARI

Reputation: 43

hostel allocation system php function

I'm writing a balloting hostel allocation system.The system is suppose to assign number to student when they enter their reg number. reg Number--------

on submit: the system should assign a unique value from 0-300: //this made me to use the code below

$random = rand(0,299);//this assign a unique random number

if($random <= 50){
 echo " you have being allocated a room "
}else{
 echo "No  room is allocated to u"

}

how do i make sure that two candidate does not receive the same random number.

Tanks alot for the help

Upvotes: 0

Views: 1075

Answers (3)

Arun Chouhan
Arun Chouhan

Reputation: 54

$already_assigned_rooms = array(201,100,400);// get database to assigned room numbers array

while( in_array( ($random = mt_rand(1,300)), $already_assigned_rooms ) );

if($random){
 echo " you have being allocated a room "
}else{
 echo "No  room is allocated to u"
}

Upvotes: 0

Erty Seidohl
Erty Seidohl

Reputation: 4579

If you absolutely cannot use a database, perhaps use the time() function plus some random number.

For example:

$random = time() . rand(0,1000);

This will echo a number like "1345164397568", and the odds of two people getting the same number are 1 in 1000, and only if they visit at exactly the same second.

Upvotes: 1

Femi Oni
Femi Oni

Reputation: 824

you can hardly reduce chance of conflict to zero if you are not using a database for persistence. if this records will be entered into a database table you should do mysql count on insert, if the entered records does not exceeds number of rooms, insert the new record else return message to user that there are no romms left. mysql auto-increase for room number column will help here

Upvotes: 2

Related Questions