- Home /
I am trying to Randomize my Answers?
I am making a multiple choice program that can be edited from a Xml File on completion. I can randomize the answers but can't keep the program from displaying the same answer twice. If someone could point me in the right direction I would be grateful.
Again Thank You for any help
(CONTENT HAS BEEN REMOVED AS OF 15:45 EST 5 DEC 2013)
QUESTION CLOSED THANK YOU
The most common solution for this kind of problem is to shuffle the answers. That is you would assign your answers in order from the X$$anonymous$$L, then after the array is assigned, a routine would randomly swap answers for some specified number of iterations. $$anonymous$$any post both on UA and on the net cover shuffling.
"I can randomize the answers but can't keep the program from displaying the same answer twice." Well, that's the problem with randomness :-)
Ohhhh, I see the problem. On level 4 it says "And God said 'Let there be !' and it was EPIC" but Bill $$anonymous$$urray isn't listed as the right answer.
I'm sorry, lol. On a more serious note. I would change the way you're reading the X$$anonymous$$L to use X$$anonymous$$L deserialization which will allow you to retrieve a container with a list all the questions and their choices. Then upon initialization (after deserializing) you can go through each list of choices and just use a simple randomize method.
Answer by JChilton · Dec 04, 2013 at 10:44 PM
Just shuffle them, then pull one at a time. There's a lotta different ways to do it.
List<string> answers = new List<string>();
//add each answer to list
List<string> shuffledList = new List<string>();
while(answers.Count > 1){
int pos = Random.Range(0,answers.Count);
shuffledList.Add(answers[pos]);
shuffledList.RemoveAt(pos);
}
shuffledList.Add(answers[0]);
I hope you don't $$anonymous$$d that I converted your comment to an answer. This is exactly it - the definition of pulling in items from a list randomly without duplicating items is to shuffle the list and then simply walk it item by item.
If you want to get fancy and not use an index or removal, you can use a Queue ins$$anonymous$$d of a List.
Java syntax is nearly identical to C#, so you shouldn't have any problems. Or do you mean Unityscript (which many people call Javascript)?
Also, I just have to say this:
If you're interning for a company, you are being paid to do this. So you are quite literally asking us to do your work for you, for free.
I'm sure someone here will still help you out, but yeah, it is a bit selfish to ask others to do your job for you, especially since there are so many articles on converting C# to JS and vice versa. Heck, there are even methods that do it for you automatically. For instance: http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/
If you're going to be a professional, getting other people do to your work for you, rather than using an opportunity to learn, isn't going to cut it.
Well at that point you'd not only be asking us to help you find the problem, you'd also be asking us to fix it for you, lol.
@ThePunisher is right. +1
@jsyoung43 you should learn how to strip down the program in to simple few lines of code, you'll get better help, better && quicker response, and many of the times you'll figure it your self before asking a question.
else we can see after few years you posting us 5$$anonymous$$ lines of code and asking us to fix your developed problem.
you are the one that knows your program best.
also keep in $$anonymous$$d to mark answers as solved if they helped you. It looks like this is the one that needs to be.