Link to home
Start Free TrialLog in
Avatar of learningnet
learningnet

asked on

to display random questions from the total 50

Hello Experts,

I have 50 questions in the table and I am trying to show 30 random out of the total  on the web page using the following
  NextQuestion = ((50) * Rnd()) + 1
            While (is_Already_Answered(NextQuestion) = True)
                NextQuestion = ((50) * Rnd()) + 1
            End While

This works ok however, at times it is also assigning the number '51' to the NextQuestion. I also dont know whether it would consider the number '0' as well?

please advise

thanks
kay


Avatar of ydramu
ydramu
Flag of United States of America image

Why dont you use random.Next(51).

Which we generate random numbers from 1-50...and your code will work as expected.
Avatar of learningnet
learningnet

ASKER

hello ydramu,
thanks for your comment

do you mean instead of using

NextQuestion = ((50) * Rnd()) + 1

use

NextQuestion = random.next(51)   ??

please advise as the above do not seems to have worked.

is random a built in function for vb ?

thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of ydramu
ydramu
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for your amendments, this seems to have worked for me.

however, a slight problem, perhaps i am doing something wrong.

can you please let me know whether the statement below will get unique random numbers?

NextQuestion = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))

I mean, does this automatically picks up the unique numbers between 1 and 50 ?

For some reason, I am showing the same question twice on the page.

please advise
The code will not generate random number by itself.

Your code now may become like this.

upperbound = 50
lowerbound = 1
While (is_Already_Answered(NextQuestion) = True)
     Randomize()
     NextQuestion = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
End While

If I am not wrong, I expected the function you have is_Already_Answered(NextQuestion) will provide true, if it is a duplicate, isn't it?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello ydramu:
yes i am having a function which returns true if already already, i was thinking if i could get unique random numbers then i could remove that check
ragi0017:
thanks for your code sample, if i wanted to implement your code can i use something like this

  While (is_Already_Answered(NextQuestion) = True)
                NextQuestion = GetRandomQuestion()
            End While

can someone of you please advise?

thanks both for your help

    Protected Function GetRandomQuestion()
        Try
       Dim Rnd As New System.Random()
    
    Dim list As New ArrayList()
     For i As Integer = 0 To 9
         Dim val As Integer = Rnd.[Next](1, 50)
        
         If list.Contains(val) Then
             While True
                 val = Rnd.[Next](1, 50)
                 If list.Contains(val) = False Then
                   list.Add(val)
                End If
             End While
         Else
             list.Add(val)
         End If
     Next
 
 
        Catch ex As Exception
 
        End Try
 
        Return   list.Add(val)
    End Function

Open in new window

hi we have jsut given you the logic on which you can build up as you please
i think (just looking at it) your logic also looks good
ok thanks can you not please advise me how to implement this in my code?


ydramu:

please can you help me here?

thanks
this still not returning unique random numbers

please advise
Protected Function GetRandomQuestion() As Integer
        Dim RetRandomNumber As Integer
        Try
            Dim Rnd As New System.Random()
 
            Dim list As New ArrayList()
            For i As Integer = 0 To 9
                Dim val As Integer = Rnd.[Next](1, 50)
 
                If list.Contains(val) Then
                    While True
                        val = Rnd.[Next](1, 50)
                        If list.Contains(val) = False Then
                            RetRandomNumber = val
                        End If
                    End While
                Else
                    RetRandomNumber = val
                End If
            Next
 
 
        Catch ex As Exception
 
        End Try
 
        Return RetRandomNumber
    End Function

Open in new window

http://www.codetoad.com/vb_random.asp

Please have a look at this code. It has the logic to get the RandomNumber using Rnd() and then verifying whether it is duplicate or not.

hi, if you use my coding logic are you not getting unique numbers?
hello ydramu

It gave me an error saying object cannot be used as integer ... (please see attached)

NextQuestion = RandomNumbers(50, 1, 1)

ragi007,
yes it seems like its not doing it right, please see below
NextQuestion = GetRandomQuestion()
 Protected Function GetRandomQuestion() As Integer
        Dim RetRandomNumber As Integer
        Try
            Dim Rnd As New System.Random()

            Dim list As New ArrayList()
            For i As Integer = 0 To 9
                Dim val As Integer = Rnd.[Next](1, 50)

                If list.Contains(val) Then
                    While True
                        val = Rnd.[Next](1, 50)
                        If list.Contains(val) = False Then
                            RetRandomNumber = val
                        End If
                    End While
                Else
                    RetRandomNumber = val
                End If
            Next


        Catch ex As Exception

        End Try

        Return RetRandomNumber
    End Function
 Public Function RandomNumbers(ByVal Upper As Integer, _
    Optional ByVal Lower As Integer = 1, _
    Optional ByVal HowMany As Integer = 1, _
    Optional ByVal Unique As Boolean = True) As Object
        '*******************************************************
        'This Function generates random array of 
        'Numbers between Lower & Upper
        'In Addition parameters can include whether 
        'UNIQUE values are required
 
        'Note the Result is INCLUSIVE of the Range
 
        'Debug Example:
        'x = RandomNumbers(49, 1, 7)
        'For n = LBound(x) To UBound(x): Debug.Print x(n);: Next n
        'WARNING HowMany MUST be greater than (Higher - Lower)
        '******************************************************
 
        On Error GoTo LocalError
        If HowMany > ((Upper + 1) - (Lower - 1)) Then Exit Function
        Dim x As Integer
        Dim n As Integer
        Dim arrNums() As Object
        Dim colNumbers As New Collection
 
        ReDim arrNums(HowMany - 1)
        With colNumbers
            'First populate the collection
            For x = Lower To Upper
                .Add(x)
            Next x
            For x = 0 To HowMany - 1
                n = RandomNumber(0, colNumbers.Count + 1)
                arrNums(x) = colNumbers(n)
                If Unique Then
                    colNumbers.Remove(n)
                End If
            Next x
        End With
        colNumbers = Nothing
        RandomNumbers = arrNums
        Exit Function
LocalError:
        'Justin (just in case)
        RandomNumbers = ""
    End Function
 
 
    Public Function RandomNumber(ByVal Upper As Integer, _
    ByVal Lower As Integer) As Integer
        'Generates a Random Number BETWEEN the LOWER and UPPER values
        Randomize()
        RandomNumber = Int((Upper - Lower + 1) * Rnd + Lower)
    End Function

Open in new window

SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial