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(NextQ uestion) = 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
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(NextQ
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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(NextQ uestion) = 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(NextQu estion) will provide true, if it is a duplicate, isn't it?
Your code now may become like this.
upperbound = 50
lowerbound = 1
While (is_Already_Answered(NextQ
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(NextQu
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(NextQ uestion) = True)
NextQuestion = GetRandomQuestion()
End While
can someone of you please advise?
thanks both for your help
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(NextQ
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
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
i think (just looking at it) your logic also looks good
ASKER
ok thanks can you not please advise me how to implement this in my code?
ydramu:
please can you help me here?
thanks
ydramu:
please can you help me here?
thanks
ASKER
this still not returning unique random numbers
please advise
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
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.
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?
ASKER
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Which we generate random numbers from 1-50...and your code will work as expected.