Randomising a Survey

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

Randomising a Survey

Post by ac11ca » Wed May 14, 2008 11:58 am

Hi all,

I've been playing around with a few ways of doing the following, but am running into problems and am running out of time to get it done. I'm sure that there is a simpler way ...

Basically I have a survey with 10 questions. Each question has beside it a drop down menu (combo box?) with the numbers 1 to 7 with default being blank. All the questions are shown on the one page. Im not sure if the questions should be put in a field, or a tablefield (which I am less familair with).

I would like the survey questions to be randomised in order each time the program runs.

The information I would like to collect is what choice was made for each question, so eventually I end up with a text file that looks like this:

Question Response
1 3
2 4
3 1
4 1
5 2
6 5
7 3
8 3
9 4
10 1

So obviously I need to have each question silently labelled as 1, 2, 3 ... etc, but I don't want the user to see the numbers while completing the survey. Also, some function to re-order the questions as shown above after the survey is completed would be handy.

Thanks for any help,
Adrian.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed May 14, 2008 5:21 pm

Hi ac11ca

I've put a little stack together which I trust will sort most of your probs.

It's in User Spaces (General) in RevOnline and is called Random Questions.

:)

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Post by Nonsanity » Wed May 14, 2008 6:32 pm

A simple list shuffler...

Code: Select all

function ShuffleList theList
	-- remove any blank lines from front or end of list
	put word 1 to (the number of words in theList) of theList into theList
	put "" into toList
	-- shuffle
	put the number of lines in theList into numLines
	repeat with a = 1 to numLines
		put random( the number of lines in theList ) into x
		put line x of theList into line a of toList
		delete line x of theList
	end repeat
	return toList
end ShuffleList 
(This code was written here and not tested.)
~ Nonsanity
~ Chris Innanen

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed May 14, 2008 9:07 pm

Adrian and others,

To shuffle a list, use:

sort lines of myList by random(number of lines of myList)

It may not be really random, but it should do the trick for making randomly sorted lists.

Adrian, is this the only problem? It looks like you have also difficulties putting together the interface. What exactly are you trying to achieve? What makes you doubt whether to use a tablefield or not?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Post by Nonsanity » Wed May 14, 2008 9:18 pm

Thank you for that one Mark! Once again, the Revolution docs SUCK. Even knowing you can do that, there's only the faintest trace of it in the docs for the sort command, down in the tip, and even that is a stretch.

I loved HyperCard back in the 80's, and to that extent I can love Revolution. But I can also hate many parts of the Revolution experience at the same time...
~ Nonsanity
~ Chris Innanen

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed May 14, 2008 11:06 pm

Hi ac11ca: hope you don't mind me diverting your post for a short while...

Hi Mark
It may not be really random, but it should do the trick for making randomly sorted lists.
I'm somewhat intrigued by this random lark: when I was trying out the demo of RB last year, there was a post concerning random on their Forum, and I remember the same thing was said/discussed (although I can't remember the explanation :wink:) .

So please could you tell me: why isn't random in basic-like languages really random; why would "almost random" be good enough; and what is the significance of the large number in the random seed example? (By experimentation, it doesn't seem to be the amount of random items...what I'm trying to say there is I still don't quite understand what a random seed is).

:)

ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

Post by ac11ca » Wed May 14, 2008 11:22 pm

Thanks gyroscope ... could you please direct me on where exactly I go to find "User Spaces (General) in RevOnline" to see the program you wrote (and thank you by the way!)

Thanks Mark and Nonsanity for the randomising code. Im not sure, however, where I actually put the code "sort lines of myList by random(number of lines of myList)"

And yes Mark, I have a few conceptual problems. Let's say this is what I want:
- 10 questions in a table field that randomly order on preopen card.
- a drop-down-menu-style option box next to each question with 7 options.
- on leaving the card I want to know what option was selected for each question

Im not sure about the "drop-down-menu-style option box . Ive never used a combo box ... how do I record what option was highlighted? And if the tablefield is randomising each timel does this mean each of the combo boxes must follow the question it is tied to in order to keep the connection between question number and response.

The other problem is time ... this was meant to be done by today >.<

Thanks for all your help guys!
Adrian.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed May 14, 2008 11:50 pm

You're welcome! No probs to find RevOnline: it lives to the left of Documentation under the main Menu Bar in the Rev prog.

Good luck with your deadline!!

:)

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Post by Nonsanity » Thu May 15, 2008 12:17 am

gyroscope wrote:So please could you tell me: why isn't random in basic-like languages really random; why would "almost random" be good enough; and what is the significance of the large number in the random seed example?
Any random number generator in a computer isn't TRULY random. The bits in a computer's memory follow known patterns. In order to get something that SEEMS random, you need to have a function that generates a pattern that is so complex and evenly distributed, that it can't be told from "real" random.

There are many different algorithms to generate random numbers, and you can read about them in Wikipedia for more detailed information. The quality of their output varies. Some are not good enough to be used for cryptography, for example. Most use the last number (or last N numbers) to generate a new output number. The "seed" is a starting number to get the ball rolling.

If you set a seed to 12345 then pull out 5 random numbers, then reset the seed to 12345 and pull out 5 more random numbers, you'll get the same 5 both times. This can be a pretty handy trick. I once wrote a program that generated a whole universe of stars and planets on the fly. You use the galaxy, the star, the planet, and other inputs to create a unique seed, then pull out "random" numbers (that are the same every time for that seed) and shape them to describe that planet. You can have a nearly infinite universe that way with no data storage at all. (You just can't CHANGE any of it...)
~ Nonsanity
~ Chris Innanen

ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

Post by ac11ca » Thu May 15, 2008 2:54 am

Thanks gyroscope! The program you wrote was extremely helpful!

One last question. I've got a button at the button of the page saying "Next" ... is there some way to make a pop-up come up on screen when the "Next" button is clicked before all of the questions have been answered (i.e., all pulldown menu's have a number in them?)

Cheers,
Adrian.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Thu May 15, 2008 12:18 pm

Hi Nonsanity,
that info has cleared up some of my confusion there, thank you!

Hi ac11ca,
If I understand your question correctly, you want a message telling the user that they're not finished yet but when all 10 choices have been made then go to the next card?

OK, here's some scripting for that (it might not be as refined as some of the more experienced scripters would do, but it works :wink:):

• In the stack script, set up some globals, i.e: global gPick1, gPick2..... etc
• In the preOpenStack handler, set them all to 0
• In your menu buttons, use this script (changing the numbers accordingly, depending on which button it is)

Code: Select all

    global gPick1
on menuPick whichPick
    if gPick1 = 0 then
        set the label of me to whichPick
        put "1   " & whichPick into line 2 of field "Results"
        put 1 into gPick1
    else
        if gPick1 = 1 then
            set the label of me to whichPick
            put "1   " & whichPick into line 2 of field "Results"
        end if
    end if
end menuPick 
• And then in your Next button, this script should do the trick:

Code: Select all

global gPick1, gPick2, gPick3, gPick4, gPick5, gPick6, gPick7, gPick8, gPick9, gPick10

on mouseUp pMouseBtnNo
    if gPick1 = 0 or gPick2 = 0 or gPick3 = 0 or gPick4 = 0 or gPick5 = 0 \
    or gPick6 = 0 or gPick7 = 0 or gPick8 = 0 or gPick9 = 0 or gPick10 = 0 then
    answer "Please complete all the questions"
else
   if gPick1 = 1 and gPick2 = 1 and gPick3 = 1 and gPick4 = 1 and gPick5 = 1 \
    and gPick6 = 1 and gPick7 = 1 and gPick8 = 1 and gPick9 = 1 and gPick10 = 1 then
   
   -- reset globals so you can use them again on next card
   -- can't use a repeat loop if the variables are global
   put 0 into gPick1
  -- etc
   go next card
    end if
end if
end mouseUp
Hope that's helpful.

:)

PS It's well worth running through all of the videos and stack scripts in Getting Started within Documentation, if you haven't already!

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Post by Nonsanity » Thu May 15, 2008 6:25 pm

To keep the code from being dependent on a particular number of questions, set up a global variable that has the number of questions in it, and one with 0 in it to mark how many have been answered.

As each choice is made, increment the counter. Display an answer dialog when the next button is clicked if the two numbers are not yet equal. (Or disable the button until the two numbers become equal.)

Just make sure that each question increments the count only once, and doesn't increment it again if the user changes their answer.

And if discrete globals are needed for each question, keep them in a list instead of using separate variables:

Code: Select all

if item 3 of gPicks = 0
...instead of...

Code: Select all

if gPick3 = 0
...which would mean gPicks might look like...

Code: Select all

1,2,4,3,3,2,0,0,0,0
...if they've answered the first 6 questions.
~ Nonsanity
~ Chris Innanen

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu May 15, 2008 10:32 pm

Adrian,

You can put the sorting code into the preOpenCard handler:

Code: Select all

on preOpenCard
  put fld "List of Questions" into myList
  sort lines of myList by random(number of lines of myList)
  put myList into fld "List of Questions
  pass preOpenCard
end preOpenCard
I don't think you want to use a combobox. You probably want to use an option menu button. Even better might be to use alternately visible list field, but this would probably be too complex for now.

It would probably be best to set a custom property when the user selects an item from the option menu button. In the preOpenCard handler, you set these custom properties to empty. In a script that eventually goes to the next card, you check whether any of the custom properties is empty, i.e. the user hasn't made a choice yet.

I don't think that the open menu buttons need to change places after the list of questions is shuffled. Option menu button 1 will always correspond to question one, only the question may be different.

I believe that gyroscope's code is way too complex for such a simple task.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

Post by ac11ca » Mon May 19, 2008 1:26 pm

Thanks again for your input everyone. (Mark, you should see my own coding for a demonstration of how to overcomplicating things).

For the time being I am going to stick with what works, and when holidays come in a few months I will see if I can't sharpen the coding (as well as my skills :P ).

Cheers,
Adrian.

Post Reply