Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

ASP question regarding randomising questions on a quiz

  • 20-02-2012 5:00pm
    #1
    Registered Users, Registered Users 2 Posts: 17,974 ✭✭✭✭


    On my site I have a quiz written in asp.net code, how would I go about randomising the questions? Sort in an array and call it with some sort of logic.

    Also looking to insert a timer, how would I go about that?


    Cheers in advance.


Comments

  • Closed Accounts Posts: 1,759 ✭✭✭Dr.Silly


    On my site I have a quiz written in asp.net code, how would I go about randomising the questions? Sort in an array and call it with some sort of logic.

    Also looking to insert a timer, how would I go about that?


    Cheers in advance.

    I assume your questions are coming from a SQL database ?
    Maybe use your stored proc to return random records ?


  • Registered Users, Registered Users 2 Posts: 17,974 ✭✭✭✭Gavin "shels"


    Nope the questions aren't stored in a database at all, just hard coded in ASP and arrays are used. Why we have it done is we have 5 arrays, one for each option of an answer and then one for the correct answer.


  • Registered Users, Registered Users 2 Posts: 851 ✭✭✭TonyStark


    Nope the questions aren't stored in a database at all, just hard coded in ASP and arrays are used. Why we have it done is we have 5 arrays, one for each option of an answer and then one for the correct answer.

    I guess that's why your asking about randomising questions. *sigh*

    Anyway look at some of the security namespaces they provide better 'randomness'.

    You really need just to have a table of indexes that you can shuffle accordingly and do lookups on your parallel arrays.

    However if I were you I'd probably create a class to encapsulate the question and build it using a more OO approach.

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        internal class Question
        {
            /// <summary>
            /// 
            /// </summary>
            private readonly List<QuestionAnswer> _questions = new List<QuestionAnswer>();
            /// <summary>
            /// Gets or sets the question text.
            /// </summary>
            /// <value>The question text.</value>
            /// <remarks></remarks>
            public string QuestionText { get; set; }
    
            /// <summary>
            /// Gets the question answers.
            /// </summary>
            /// <remarks></remarks>
            public List<QuestionAnswer> QuestionAnswers
            {
                get { return _questions; }
            }
        }
    
        internal class QuestionAnswer
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="QuestionAnswer"/> class.
            /// </summary>
            /// <param name="t">The t.</param>
            /// <param name="b">if set to <c>true</c> [b].</param>
            /// <remarks></remarks>
            public QuestionAnswer(string t, bool b)
            {
                Text = t;
                IsCorrect = b;
            }
    
            /// <summary>
            /// Gets or sets the text.
            /// </summary>
            /// <value>The text.</value>
            /// <remarks></remarks>
            public string Text { get; set; }
            /// <summary>
            /// Gets or sets a value indicating whether this instance is correct.
            /// </summary>
            /// <value><c>true</c> if this instance is correct; otherwise, <c>false</c>.</value>
            /// <remarks></remarks>
            public bool IsCorrect { get; set; }
        }
    
        internal static class Extensions
        {
            /// <summary>
            /// Shuffles the specified list.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list">The list.</param>
            /// <remarks></remarks>
            public static void Shuffle<T>(this IList<T> list)
            {
                var provider = new RNGCryptoServiceProvider();
                int n = list.Count;
                while (n > 1)
                {
                    var box = new byte[1];
                    do provider.GetBytes(box); while (!(box[0] < n*(Byte.MaxValue/n)));
                    int k = (box[0]%n);
                    n--;
                    T value = list[k];
                    list[k] = list[n];
                    list[n] = value;
                }
            }
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <remarks></remarks>
        class Program
        {
    
            /// <summary>
            /// Mains the specified args.
            /// </summary>
            /// <param name="args">The args.</param>
            /// <remarks></remarks>
            static void Main(string[] args)
            {
                Question q1 = new Question();
                q1.QuestionText = "What is the capital of Ireland?";
    
                q1.QuestionAnswers.Add(new QuestionAnswer("Dublin", true));
                q1.QuestionAnswers.Add(new QuestionAnswer("Paris", false));
                q1.QuestionAnswers.Add(new QuestionAnswer("London", false));
    
                Question q2 = new Question();
                q2.QuestionText = "What is the odd one out?";
    
                q2.QuestionAnswers.Add(new QuestionAnswer("Potato", true));
                q2.QuestionAnswers.Add(new QuestionAnswer("Plum", false));
                q2.QuestionAnswers.Add(new QuestionAnswer("Apple", false));
    
                Question q3 = new Question();
                q3.QuestionText = "What is a common pet?";
    
                q3.QuestionAnswers.Add(new QuestionAnswer("Catr", true));
                q3.QuestionAnswers.Add(new QuestionAnswer("Lion", false));
                q3.QuestionAnswers.Add(new QuestionAnswer("Gorrilla", false));
    
                List<Question> questions = new List<Question>();
                questions.Add(q1);
                questions.Add(q2);
                questions.Add(q3);
    
                // print them out in the correct order
                foreach (var question in questions)
                {
                    Console.WriteLine(question.QuestionText);
                }
    
                Console.WriteLine();
                Console.WriteLine("Shuffling..");
                Console.WriteLine();
                
    
                questions.Shuffle();
    
                // print them out shuffled
                foreach (var question in questions)
                {
                    Console.WriteLine(question.QuestionText);
                }
    
                Console.ReadKey();
            }
    
    
        }
    }
    
    
    


  • Registered Users, Registered Users 2 Posts: 17,974 ✭✭✭✭Gavin "shels"


    Cheers Tony. Any idea how I'd go about the below, have googled it and there seems to be so many different ways, I just want to give a timer of about 10 mins for the whole quiz.

    Also looking to insert a timer, how would I go about that?


  • Registered Users, Registered Users 2 Posts: 851 ✭✭✭TonyStark


    Cheers Tony. Any idea how I'd go about the below, have googled it and there seems to be so many different ways, I just want to give a timer of about 10 mins for the whole quiz.

    Also looking to insert a timer, how would I go about that?


    It depends on how secure you want it. You could store the start time in session and then display this on the front end. You would need some ajax on the front end to poll the time and then cause it to expire when the time has elapsed.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 17,974 ✭✭✭✭Gavin "shels"


    TonyStark wrote: »
    It depends on how secure you want it. You could store the start time in session and then display this on the front end. You would need some ajax on the front end to poll the time and then cause it to expire when the time has elapsed.

    Doesn't need to be secure at all tbh, only a college project and most likely won't go live.


Advertisement