randomly order a list

Jacques Hausser jacques.hausser at unil.ch
Wed May 22 18:35:59 EDT 2013


Chris,

I think that you cannot sort by random(999999) because the argument of the sort (the sortkey as it is called in the dictionary) must by something like "item 2 of each" and not a number out from a random generator. 

If you want a random order of the three lines of possible answers, you could for instance:

--1) put the different possible orders in a string (6 possible orders for three items)

put "1,2,3" & cr & "2,1,3" & cr & "2,3,1" & cr & "1,3,2" & cr & "3,1,2" & cr & "3,2,1" into sOrderOfLines

(I keep the comma as item delimiter for typographic clarity - if you need a tab, you should replace commas by tabs in sOrderOfLines too, to avoid frequent switches of itemdels):

--2) then in your loop:

put "*" & item 3 of sRecSet into tPossibleAnswers -- correct answer
put cr & item 4 of sRecSet after tPossibleAnswers -- distractor 1
put cr & item 5 of sRecSet after tPossibleAnswers -- distractor

put line(random(6)) of sOrderOfLines into tOrder
repeat with i = 1 to 3
   put line(item i of tOrder) of tPossibleAnswers into line i of fld"YourField"
next repeat

It's wrong to use numbers larger than needed (here, 6) into the random function: they will only call for non existing items.
Remember that a sequence of random numbers means only that it is impossible, given a part of this sequence of numbers, to predict the next number(s) in the sequence and that every number in the sequence has the same probability to be the next one. Thus with only 6 possibilities it is quite possible to have  the same order several times in succession. I you don't want that, you could 

put "" into tLastOrder
put "0,0,0" into tOrder -- or anything you want, except the "real" possible orders and ""
repeat until tOrder <> tLastOrder
   put line(random(6)) of sOrderOfLines into tOrder
next repeat
put tOrder into tLastOrder
repeat with i = 1 to 3
   put line(item i of tOrder) of tPossibleAnswers into line i of fld"YourField"
next repeat

so you'll never have the same order for two successive questions - and the general sequence will remain random (you can predict that given order will not appear twice, but not which one from the other orders will appear next).

a better possibility would be to check only for the position of the correct answer: you'll have to check this position with offset function.

hth

Jacques


Le 22 mai 2013 à 23:34, Chris Sheffield <cmsheffield at icloud.com> a écrit :

> Thanks for the suggestions everyone, but I'm still getting strange results, and I'm beginning to think there's something I'm doing that's affecting use of the random() function. Not really sure what it would be though. Here's my code:
> 
> set the itemDel to tab
> put "*" & item 3 of sRecSet into tPossibleAnswers -- correct answer
> put cr & item 4 of sRecSet after tPossibleAnswers -- distractor 1
> put cr & item 5 of sRecSet after tPossibleAnswers -- distractor 2
> sort lines of tPossibleAnswers by random(999999) -- randomly re-order the list
> 
> This app pulls words from a database and presents three possible answers to choose from. The asterisk above is used to identify the correct answer after the sort takes place. It's removed later on. The sort only works randomly one time. After that, the same sort order is used every time, so the correct answer *always* ends up listed first. It doesn't matter if I use a very high number or if I use 'the number of lines of tPossibleAnswers'. Something is very strange. Just as a quick test, I added a button to the card with this inside:
> 
> put "one" & cr & "two" & cr & "three" into tLines
> sort lines of tLines by random(the number of lines of tLines)
> answer tLines
> 
> This seems to work just fine. Yes, it does mean getting the same order sometimes twice or maybe even three times in a row, but not usually more than that, which would be fine in this case. So I'm not sure what's going on with my actual code. I'm no longer setting the randomSeed or anything like that.
> 
> The other strange thing is no matter what I try, if I use the random() function or the any keyword in anyway, I get similar results. Something is affecting the "randomness". This is for an iOS app, btw, if that makes any difference to anyone. This might be kind of a dumb thing to do, but the only other thing I can think of would be to add several more lines of data to the three actual possible answers, then sort the whole thing, then somehow filter out everything I added before the sort. With more lines, maybe I'd get better results?
> 
> Thanks again,
> Chris
> 
> On May 22, 2013, at 3:03 PM, Dar Scott <dsc at swcp.com> wrote:
> 
>> I think you are going to get the first line of the original list (correct answer) about half the time.  Does that seem right to you from what you have seen?  The correct answer will be in the first two about 80% of the time.  
>> 
>> Using the larger argument for random should give you better proportions.  You should get it in the first line a third of the time.  
>> 
>> If you only interested in the first line, there might be some methods that are clearer and more fun.
>> 
>> Dar
>> 
>> 
>> On May 22, 2013, at 11:59 AM, Chris Sheffield wrote:
>> 
>>> I have a list of three words that I need to be randomly sorted. To start with, the first word is the correct answer to a question. I want to re-order the list so that the correct answer may be the second or third word, and not necessarily the first. How can I do this successfully every time? The docs give an example like this:
>>> 
>>> 	sort lines of myVar by random(the number of lines of myVar)
>>> 
>>> But this only seems to work successfully one time. After that, the list is always set so the first word is the correct answer. So then I tried randomly setting the randomSeed value, since this value is supposed to affect the random() function and the any keyword, but this didn't seem to make much difference except to change it so either the second or third word is *always* the right answer. I need it to be more mixed up than that.
>>> 
>>> So does anyone have a good way to do this?
>>> 
>>> Thanks,
>>> Chris
>>> 
>>> 
>>> --
>>> Chris Sheffield
>>> Read Naturally, Inc.
>>> www.readnaturally.com
>>> 
>>> 
>>> _______________________________________________
>>> use-livecode mailing list
>>> use-livecode at lists.runrev.com
>>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


E-Mail:	jacques.hausser at unil.ch
*******************************************





More information about the use-livecode mailing list