locked
Looping too much.. RRS feed

  • Question

  • User315403214 posted

    My scenario is a quiz of 10 questions(ATM, but is dynamic), so I do a randomly choose a number out between 1-10(code below), but it seems like it hangs when you get down to the 8-9th question when looping to get the last question number. Any suggestions on making this more effient?

     

     Dim l_intNumbers As New System.Collections.Generic.List(Of String)()
    
    
                    'Random on Questions      
                    Dim RandomClass As New Random()
                    Dim RandomNumber As Integer
    
                    Do
                        RandomNumber = RandomClass.Next(1, count + 1)
    
                        If Session("QuestionNums") IsNot Nothing Then
                            l_intNumbers = CType(Session("QuestionNums"), System.Collections.Generic.List(Of String))
                        End If
    
                    Loop While l_intNumbers.Contains(RandomNumber)
    
                    ' RandomNumber is a number not in the list. add it.   
                    l_intNumbers.Add(RandomNumber)
    
    
                    Session("QuestionNums") = l_intNumbers


     

    Monday, December 7, 2009 12:52 PM

Answers

  • User877117016 posted

    Are there more than one process accessing this session variable at the same time?

    If so you could have potential deadlocks, where there is only one value left and one thread goes into the loop, then another thread goes in, then the second thread adds the value first, and the first thread loops forever.

    If not, this might help: 

    Dim l_intNumbers As New System.Collections.Generic.List(Of String)()   
      
      
                   'Random on Questions         
                   Dim RandomClass As New Random()   
                   Dim RandomNumber As Integer                    If Session("QuestionNums") IsNot Nothing Then                         l_intNumbers = CType(Session("QuestionNums"), System.Collections.Generic.List(Of String))                      End If  
                   Do  
                       RandomNumber = RandomClass.Next(1, count + 1)   
                         
                   Loop While l_intNumbers.Contains(RandomNumber)   
      
                   ' RandomNumber is a number not in the list. add it.      
                   l_intNumbers.Add(RandomNumber)   
      
      
                   Session("QuestionNums") = l_intNumbers  
    
    
    

    If you want a list of randomly sorted numbers 1-10, I like to use something like this:

    int[10] a = new int[] for(int i = 0; i < a.Length; i++) { a[i] = i; } int x, y; int tempHolder; Random ran = new Random(); int factor = 2; // or you could make it factor = Math.Log(a.Length) to get O(nlogn) for(int j = 0; j < factor * a.Length; j++) { x = ran.NextInt(a.Length); y = ran.NextInt(a.Length); tempHolder = a[x]; a[x] = a[y]; a[y] = tempHolder; } 

    What it does is to put 1-10 in an array, then randomly permute them around. The code above changes every character on average almost 4 times.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 7, 2009 1:51 PM