Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [help] Javascript Random Generator

    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)

    Code:
    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
    Code:
    var i = Math.round(6*Math.random());
    document.write(stemarray[i]);
    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

    answer].

    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

    ~M~
    Last edited by mamamiaw; 08-03-2008 at 06:58 AM.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Here's a somewhat accurate pseudo random number generator, though you do have to convert to javascript.

    There is also this analysis.

    And a simple javascript version, with this idea of random questions in mind.
    Last edited by fside; 08-03-2008 at 04:13 AM.

  • Users who have thanked fside for this post:

    mamamiaw (08-03-2008)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i see...
    thanks....
    but, about the random methods, i still can't find the perfect solution

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •