View Full Version : [help] Javascript Random Generator

08-03-2008, 01:37 AM
Hi all, Sorry to bother u all, I think i previously post about the quiz script, and i read and research abit here and there,

anyway i manage to write some but found some difficulties,especially about random generator..

I have this several 'set' of question and answer, and i like to view'em random
(e.g: refresh---> What is 1+1=?...with option n aswers, refresh---> What is 1+1=?...with option n aswers, refresh---> What is

1+1=?...with option n aswers, ...etc)

questionarray[0] ='What is 1+1=?.'
answer[0] ='1'
Adistract[0] ='2'
Bdistract[0] ='22'
Cdistract[0] ='11'
Ddistract[0] ='0'
questionarray[1] ='what is the color of the sky?.'
answer[1] ='C'
Adistract[1] ='red'
Bdistract[1] ='yellow'
Cdistract[1] ='blue'
Ddistract[1] ='purple'
questionarray[2] ='cow is =?.'
answer[2] ='D'
Adistract[2] ='Tank'
Bdistract[2] ='President'
Cdistract[2] ='Weapon'
Ddistract[2] ='Animal'
questionarray[3] ='What is 2+2=?.'
answer[3] ='A'
Adistract[3] ='4'
Bdistract[3] ='22'
Cdistract[3] ='11'
Ddistract[3] ='0' .....etc

i know this can be done by

var i = Math.round(6*Math.random());

but when i use that, i only get the question...i can't randomly view them as 'set' [question,option of answers and the right


and also if any of master n gurus here have better suggestion, both on the question array and get (viewing method on browser),

i'll be very thankful.
Anyway, thanks in advance.

Best Regards


08-03-2008, 04:08 AM
Here's a somewhat accurate pseudo random number generator (http://local.wasp.uwa.edu.au/~pbourke/other/random/randomlib.c), though you do have to convert to javascript.

There is also this analysis (http://www.robertnz.net/true_rng.html).

And a simple javascript version (http://javascriptkit.com/script/script2/motivatequotes.shtml), with this idea of random questions in mind.

Philip M
08-03-2008, 07:53 AM
Be careful in assigning numeric values to strings (in quotes).

var x = "2"; // x is a string
y = x + 2;
alert (y); // = 22, values are concatenated not added

var x = 2; // x is a number
y = x + 2;
alert (y); // = 4

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

08-03-2008, 01:13 PM
i see...
but, about the random methods, i still can't find the perfect solution

08-03-2008, 04:23 PM
The last link, above (and the next to last line of the script on that page). That's the simplest one-line solution. But it may not be the most random. That's why I mentioned the others, above it. But you may not care about that, only that it seem fairly random. It may be just good enough?