selecting random values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
justphp786
Forum Newbie
Posts: 2
Joined: Sat May 15, 2004 9:59 am

selecting random values

Post by justphp786 »

Dear all:

I have a application where I have to show 50 odd questions from the table one at a time. The question each time should be randomly selected and previously selected questions should not be reselected. I am able to generate random number with srand and rand functions.

How do I store questions previously selected?

Please giude.

Justphp786
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Some questions ;)
1. Is this per person or 'global'? ie If i get one set of 50 questions, then my set must change next time, or if i get one set of 50 questions then everybody else must also get a different set when they load the page?
2. If it's per person, how does you app identify a person? ie is there a login system, using session, using cookies etc.. (just so we can use an existing storage method to store the questions already seen, if one exists).
3. What happens when i've seen every question is the database? Does it start again from scratch with a new random set of questions?
justphp786
Forum Newbie
Posts: 2
Joined: Sat May 15, 2004 9:59 am

Post by justphp786 »

Answers to best of my capacity!

I have table with 200 questions and every time an user demands a question a select statement will fetch any random question from the table.

so the select statement may look like this

srand((double)microtime()*100000);
$num=rand(1,50);
select * from mytable where qnum=$num;
now if the $num is already used then it should again create another random number so that same question will not be selected.
How do we store these $num values? can we store array as cookie.
Post Reply