How do I fix a problem with array that is storing the same value in the other indexes?

  • This code should populate an array with random values that were loaded from a text file, without repetition. But instead of this, some values are appearing in more than one index. Could someone help me to find the problem in this code?

    + AJAX: On "questions" completed

    -> System: Set totalQuestions to tokenat(AJAX.LastData,0,"|")

    -> questionsArray: Set size to (totalQuestions+1, 1, 1)

    -> questionsArrayTemp: Set size to (totalQuestions+1, 1, 1)

    ----+ System: controlRandom = 0

    --------+ System: For "For Each Questions Set" from 0 to questionsArrayWidth

    ---------> questionsArray: Set value at i to tokenat(AJAX.LastData,i,"|")

    ---------> System: Add 1 to i

    ----+ System: Else

    --------+ System: Repeat questionsArrayTemp.Width times

    ---------> questionsArrayTemp: Set value at LoopIndex to LoopIndex

    --------+ System: Repeat questionsArray.Width times

    ---------> System: Set i to (floor(random(questionsArrayTemp.Width)))+1

    ---------> questionsArray: Set value at LoopIndex to tokenat(AJAX.LastData,i,"|")

    ---------> questionsArrayTemp: Delete index i from X axis

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It's hard to make sense of your code without seeing the project file and the structure of txt file with questions.

    If you need a randomized array, you can initially set array width to 0. Then read a new question from AJAX and insert it at index int(random(array.width+1))

    Or you can load all questions into the array at Y=1, put random values into Y=0 and then use "array sort".

    Or load all questions into temp array, then pick a random element, push it into another array and remove from the temp array. Repeat until all questions are transferred.

  • Hi dop2000

    Thanks for the suggestions.

    I decided to use this solution:

    + AJAX: On "questions" completed

    -> System: Set totalQuestions to tokenat(AJAX.LastData,0,"|")

    -> questionsArray: Set size to (totalQuestions+1, 1, 1)

    -> questionsArrayTemp: Set size to (totalQuestions+1, 1, 1)

    ----+ System: controlRandom = 0

    --------+ System: For "For Each Questions Set" from 0 to questionsArray.Width

    ---------> questionsArray: Set value at i to tokenat(AJAX.LastData,i,"|")

    ---------> System: Add 1 to i

    ----+ System: Else

    --------+ System: For "For Each Questions Set" from 0 to questionsArrayTemp.Width

    ---------> questionsArrayTemp: Set value at i to tokenat(AJAX.LastData,i,"|")

    ---------> System: Add 1 to i

    --------+ System: Repeat questionsArray.Width times

    ---------> System: Set iTemp to int(random(1, questionsArrayTemp.Width))

    ---------> questionsArray: Set value at LoopIndex+1 to questionsArrayTemp.At(iTemp)

    ---------> questionsArrayTemp: Delete index iTemp from X axis

    And it worked very well. I just don't understand why the other solution generated so weird results. But this is not a problem.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)