Results 1 to 11 of 11

Thread: random questions w/o repetition

  1. #1
    Join Date
    Dec 2012
    Posts
    8
    Qt products
    Qt3 Qt5
    Platforms
    Windows Symbian S60

    Question random questions w/o repetition

    hi newbie here and even in QT... i can't work on non-repeating question to display...please help

    Qt Code:
    1. function riddles1() {
    2. var db = openDatabaseSync("riddleDB","1.0","riddle database", 10000)
    3. var rs
    4.  
    5. db.transaction(function(tx) {
    6. rs = tx.executeSql('SELECT * FROM riddle')
    7. })
    8.  
    9. //randomized riddle
    10. for ( var i = 1; i < (rs.rows.length - 1); i++) {
    11. var n = Math.floor(Math.random() * (rs.rows.length))
    12.  
    13. }}
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: random questions w/o repetition

    what do you mean by var!?

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: random questions w/o repetition

    looks like c#. Which rather begs the question "what does this have to do with Qt?"
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: random questions w/o repetition

    Looks more like JavaScript...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: random questions w/o repetition

    Yes, Javascript the other language we had to have as part of QML. SQL local storage appears in several places in the Qt docs. Doesn't help shed any light on the OP's actual problem/question though.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: random questions w/o repetition

    I'm guessing OP wants to randomly choose a row from the database and wants assurance that rows already chosen will not be picked again.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Dec 2012
    Posts
    8
    Qt products
    Qt3 Qt5
    Platforms
    Windows Symbian S60

    Default Re: random questions w/o repetition

    var is variable... yes it's javascript... i use sql to store my riddles question. it's randomized the riddles but it repeats... how to eliminate the one that already given?

    sir, what is OP? and how to use it? no idea at all

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: random questions w/o repetition

    Only way is to keep track of questions/riddles which were already asked
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: random questions w/o repetition

    Quote Originally Posted by shinichi7 View Post
    var is variable... yes it's javascript... i use sql to store my riddles question. it's randomized the riddles but it repeats... how to eliminate the one that already given?
    Read all the questions into an array and then when a riddle is asked, remove it from the array. This way the array will always contain only unasked riddles. If you need it to be persistant, have a bool field in the database marking whether the question was asked or not and update it accordingly.

    sir, what is OP?
    Original poster --- you
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Dec 2012
    Posts
    8
    Qt products
    Qt3 Qt5
    Platforms
    Windows Symbian S60

    Default Re: random questions w/o repetition

    so i have to do it again in array format... oh my
    now i know what OP is...

    here the complete js code

    Qt Code:
    1. function riddles() {
    2. var db = openDatabaseSync ("riddleDB", "1.0", "riddle database", 10000)
    3. var rs
    4.  
    5. db.transaction(
    6. function(tx) {
    7. tx.executeSql ('CREATE TABLE IF NOT EXISTS riddle(riddle_id NUMBER,' +
    8. 'riddle_question VARCHAR2, riddle_answer VARCHAR2, score NUMBER)');
    9.  
    10. rs = tx.executeSql('Select riddle_id from riddle');
    11. if (rs.rows.length == 0) {
    12. tx.executeSql('INSERT INTO riddle VALUES(1, "What goes up but never goes down?", "Age", 10, "music.mp3")');
    13. tx.executeSql('INSERT INTO riddle VALUES(2, "What pet is always on the floor?", "Carpet", 10, "music.mp3")');
    14. tx.executeSql('INSERT INTO riddle VALUES(3, "The more you take the more you leave behind", "Footsteps", 10, "music.mp3")');
    15. tx.executeSql('INSERT INTO riddle VALUES(4, "What is the only room from which no one can enter or leave?", "Mushroom", 10, "music.mp3")');
    16. tx.executeSql('INSERT INTO riddle VALUES(5, "The person who makes it, sells it. The person who buys it never uses it and the person who uses it doesnt know they are. What is it?", "Coffin", 10, "music.mp3")');
    17. tx.executeSql('INSERT INTO riddle VALUES(6, "What goes round the house and in the house but never touches the house?", "Sun", 10, "music.mp3")');
    18. tx.executeSql('INSERT INTO riddle VALUES(7, "What is in seasons, seconds, centuries and minutes but not in decades, years or Days?", "N", 10, "music.mp3")');
    19. tx.executeSql('INSERT INTO riddle VALUES(8, "The more you have of it, the less you see. What is it?", "Darkness", 10, "music.mp3")');
    20. tx.executeSql('INSERT INTO riddle VALUES(9, "What is black when you get it, red when you use it, and white when you are all through with it?", "Charcoal", 10, "music.mp3")');
    21. tx.executeSql('INSERT INTO riddle VALUES(10, "My life can be measured in hours, I serve by being devoured.Thin, I am quick Fat, I am slow Wind is my foe.", "Fire", 10, "music.mp3")');
    22. tx.executeSql('INSERT INTO riddle VALUES(11, "I build up castles.I tear down mountains.I make some men blind,I help others to see.What am I?", "Sand", 10, "music.mp3")');
    23. tx.executeSql('INSERT INTO riddle VALUES(12, "What is it that after you take away the whole, some still remains?", "Wholesome", 10, "music.mp3")');
    24. tx.executeSql('INSERT INTO riddle VALUES(13, "I can be long, or I can be short. I can be grown, and I can be bought. I can be painted, or left bare. I can be round, or square. What am I?", "Fingernails", 10, "music.mp3")');
    25. tx.executeSql('INSERT INTO riddle VALUES(14,"What has a neck and no head, two arms but no hands?", "Shirt", 10, "music.mp3")');
    26. tx.executeSql('INSERT INTO riddle VALUES(15, "Remove six letters from this sequence to reveal a familiar English word. BSAINXLEATNTEARS", "Banana", 10, "music.mp3")');
    27. tx.executeSql('INSERT INTO riddle VALUES(16, "I am white, I am round, but not always around. Sometimes you see me, sometimes you dont. What am I?", "Moon", 10, "music.mp3")');
    28. tx.executeSql('INSERT INTO riddle VALUES(17, "I have an end but no beginning, a home but no family, a space without room. I never speak but there is no word I cannot make. What am I?", "Keybord", 10, "music.mp3")');
    29. tx.executeSql('INSERT INTO riddle VALUES(18, "I am an insect, & the first half of my name reveals another insect. Some famous musicians had a name similar to mine. What am I? ", "Beetle", 10, "music.mp3")');
    30. tx.executeSql('INSERT INTO riddle VALUES(19, "It is greater than God and more evil than the devil. The poor have it, the rich need it and if you eat it youll die. What is it?", "Nothing", 10, "music.mp3")');
    31. tx.executeSql('INSERT INTO riddle VALUES(20, "I never was, am always to be. No one ever saw me, nor ever will. And yet I am the confidence of all, To live and breathe on this terrestrial ball. What am I?", "Tomorrow", 10, "music.mp3")');
    32. tx.executeSql('INSERT INTO riddle VALUES(21, "There was a green house. Inside the green house there was a white house. Inside the white house there was a red house. Inside the red house there were lots of babies. What is it?", "Watermelon", 10, "music.mp3")');
    33. tx.executeSql('INSERT INTO riddle VALUES(22, "If you can purchase 8 eggs for 26 cents, how many can you buy for a cent and a quarter?", "Eight", 10, "music.mp3")');
    34. tx.executeSql('INSERT INTO riddle VALUES(23, "If you look you cannot see me. And if you see me you cannot see anything else. I can make anything you want happen, but later everything goes back to normal. What am I?", "Imagination", 10, "music.mp3")');
    35. tx.executeSql('INSERT INTO riddle VALUES(24, "I have a head and tail but no body. What am I?", "Coin", 10, "music.mp3")');
    36. tx.executeSql('INSERT INTO riddle VALUES(25, "When you have me, you feel like sharing me. But, if you do share me, you dont have me. What am I?", "Secret", 10, "music.mp3")');
    37.  
    38. }
    39. })
    40. }
    41.  
    42.  
    43. //Riddle_Question
    44.  
    45. function riddles1() {
    46. var db = openDatabaseSync("riddleDB","1.0","riddle database", 10000)
    47. var rs
    48. var QCount
    49. //var ridCount
    50.  
    51. db.transaction(function(tx) {
    52. rs = tx.executeSql('SELECT * FROM riddle')
    53. })
    54.  
    55. //randomized riddle
    56. for ( var i = 1; i < (rs.rows.length - 1); i++) {
    57. var n = (Math.random() * (rs.rows.length))
    58. if (null != rs.rows.length) {
    59. QCount++;
    60. }
    61. }
    62.  
    63. hidden.text = rs.rows.item(n).riddle_id
    64. txtQuestion.text = rs.rows.item(n).riddle_question
    65. ridCount++
    66. answers.text = ""
    67. }
    68.  
    69. function score1() {
    70. var db = openDatabaseSync("riddleDB","1.0","riddle database", 10000)
    71. var rs
    72.  
    73. db.transaction(function(tx) {
    74. rs = tx.executeSql('SELECT * FROM riddle where riddle_id = "' + parseInt(hidden.text) + '"')
    75. })
    76.  
    77. if (answers.text == rs.rows.item(0).riddle_answer)
    78. ridScore = ridScore + 1
    79.  
    80. if ((answers.text != rs.rows.item(0).riddle_answer))
    81. riddlemaster.state = "wrong"
    82. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: random questions w/o repetition

    You don't have to use arrays if you use the second solution I suggested.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Random markers from nowhere.
    By Spitfire in forum Qwt
    Replies: 10
    Last Post: 3rd November 2011, 14:43
  2. random alphabets
    By hema in forum Newbie
    Replies: 3
    Last Post: 20th July 2011, 17:23
  3. Possible to get random value in QMultiHash/Map?
    By weevil in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2010, 11:00
  4. Replies: 1
    Last Post: 7th April 2010, 16:26
  5. random
    By raphaelf in forum General Programming
    Replies: 9
    Last Post: 6th June 2007, 12:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.