
-----------------------------------
Wintermini
Fri Jan 08, 2010 9:44 pm

How do you randmomize strings?
-----------------------------------
Basically, I'm doing a MULTIPLE CHOICE quiz with the following questions, and so far i have this down:



        var question : array 0 .. 8 of string
        question (0) := "Approximately how big is a serving?"
        question (1) := "What is the recommended serving of Milk and Milk Alternatives for a 15-year-old girl?"
        question (2) := "Eating too much contribute to _____?"
        question (3) := "What is the type of food you shouldn't be eating too much of?"        
        question (4) := "Aside from healthy eating, what else should you do to balance your healthy lifestyle?"        
        question (5) := "Which food products belong in the Grain Products?"
        question (6) := "Which food products belong in the Meat and Meat Alternatives?"  
        question (7) := "Which food products belong in the Fruits and Vegetables?"  



-----------------------------------
deltamanx
Fri Jan 08, 2010 9:56 pm

Re: How do you randmomize strings?
-----------------------------------
You could take the long round about way of doing it and do this:

[code]
var something : int

something := randint (0, 7)

if something = 0 then
put question (0)
elsif something = 1 then
put question (1)
elsif ~~~
~~~
~~~
elsif something = 7 then
put question (7)
end if
[/code]

But I don't think you want to do that.

-----------------------------------
Ktomislav
Sat Jan 09, 2010 11:50 am

Re: How do you randmomize strings?
-----------------------------------
You mean how to make you program asks you a random question?
If so then use what deltamanx said or simplified:

[code]var temp, num_of_q: int
temp := randint (0, num_of_q-1);
put question (temp)[/code]

num_of_q is number of questions you have got so far. So for now num_of_q is 8.
The reason -1 is there is because you started your question array from 0.
Hope this helps.

-----------------------------------
Insectoid
Sat Jan 09, 2010 9:17 pm

RE:How do you randmomize strings?
-----------------------------------
The problem with that is that you can have questions repeat. Better to have a 'shuffle' function. There are different ways to do this, the easiest being an array of strings, then select 2 random elements and swap them. Do this X amount of times (I dunno exactly how big X should be, probably array size * another variable. It's hard to judge the point where it can be called 'shuffled').

Shuffling is actually remarkably similar to sorting.

-----------------------------------
Ktomislav
Sun Jan 10, 2010 5:15 am

Re: How do you randmomize strings?
-----------------------------------
If want to make sure your questions don't repeat use array which contains indexes of asked questions.
And ofcourse a counter to know how many questions your program has asked so far.

Lets say asked: array of int and  num_asked: int.
So for instance if your program asks question number five num_asked := num_asked + 1;
and asked (num_asked) := 5;
