Link to home
Start Free TrialLog in
Avatar of Real_coffee
Real_coffee

asked on

Non Repeating Random Numbers

Hi,

Could you show me where I am going wrong please?

I want to choose a number in a range between 1-10 until there are no more numbers left.

My form has a single button.

My Idea is - I create an array, populate it 1,2,3,4,5,6,7,8,910. randomly choose from the array and then remove the value that I have just chosen.

Two problems -
My page is throwing errors that al isnt declared - but it is declared in Page Load?
Will my non repeating number idea actually work? can you see any problems with it?







<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'get the size of the array 
        Dim myalsize As Integer = al.Count.ToString()
 
        'select a random number between 1 and size of array
        Dim RNDNUM As New Random
        Dim myrandindex As Integer = RNDNUM.Next(1, myalsize)
        'display the number which is going to be mye= index position
        Response.Write(myrandindex)
        
        'get the value from the array
        Dim myrandomvalue = al.Item(myrandindex - 1)
        
        'remove the selected random number value from the array
        al.Remove(myrandindex)
        
        'figure out how big the array is now
        myalsize = al.Count.ToString()
        'display the new array size
        Response.Write(myalsize)
        
        'display the new contents of the array
        For c = 1 To myalsize
            Response.Write(al.Item(c - 1))
        Next
        
 
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            'create the array called al
            Dim al As ArrayList = New ArrayList(10)
            
            'fill the array with values 1-10
            For c = 1 To 10
                al.Add(c)
                al.TrimToSize()
                'display the contents of the array
                Response.Write(al.Item(c - 1))
            Next
        End If
        
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Non Repeating Random Numbers 1 - 10 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Choose Random 1-10" />
    
    </div>
    </form>
</body>
</html>

Open in new window

Avatar of abel
abel
Flag of Netherlands image

You reference an object called "al", which is indeed not declared. Before we delve into your method, what is the object supposed to be and where should it come from? Mmm, I see it, it is declared inside the Page_Load, but that's another function. if you want it to be accessible by all functions, for this page, you need to add it outside the functions. This way it becomes a "member variable":

    Private al As ArrayList = New ArrayList(10)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       ....

you may want to choose a more recognizable name, though.
Avatar of Jamie McAllister
Got the below example from http://www.dreamincode.net/forums/showtopic17139.htm

If you change the size of the array, and change the random number range from 49 to 10 you should be in business.
Public Class Form1
    ' Dimension the variables used in the programme
    Dim intNumber As Integer
    Dim arrNumber(0 To 5) As Integer
    Dim i, x, y As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        'Make sure the Label is clear
        Label1.Text = ""
 
        'We want a total of 6 Numbers (UK Lottery)
 
        For x = 0 To 5
Start:
            Randomize()
            intNumber = Int((49 * Rnd()) + 1) ' Random number 1 to 49
            For y = 0 To 5
                ' Check arrNumber (y)
                'If intnumber has already been selected, 
                'Then go and select another one.
                If intNumber = arrNumber(y) Then
                    GoTo Start
                End If
            Next y
 
            'Place the next non-repeated number in the arrNumber(x).
            arrNumber(x) = intNumber
            
        Next x
        '----------------------------------------------------
        For i = 0 To 5
            Label1.Text = Label1.Text & (arrNumber(i)) & " , "
        Next
        
    End Sub
    
End Class

Open in new window

Avatar of Real_coffee
Real_coffee

ASKER

Ok Ive changed the code as you suggest (and changed the arrayname!).

Its now flipping out at line 14 with 'minValue' cannot be greater than maxValue. Parameter name: minValue

Line 14 is -  Dim myrandindex As Integer = RNDNUM.Next(1, myalsize)

So for some reason its not getting that myalsize?
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitionRNDArray.dtd">
 
<script runat="server">
 
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'get the size of the array 
        Dim myalsize As Integer = RNDArray.Count.ToString()
 
        'select a random number between 1 and size of array
        Dim RNDNUM As New Random
        Dim myrandindex As Integer = RNDNUM.Next(1, myalsize)
        'display the number which is going to be mye= index position
        Response.Write(myrandindex)
        
        'get the value from the array
        Dim myrandomvalue = RNDArray.Item(myrandindex - 1)
        
        'remove the selected random number value from the array
        RNDArray.Remove(myrandindex)
        
        'figure out how big the array is now
        myalsize = RNDArray.Count.ToString()
        'display the new array size
        Response.Write(myalsize)
        
        'display the new contents of the array
        For c = 1 To myalsize
            Response.Write(RNDArray.Item(c - 1))
        Next
        
 
    End Sub
    
    Private RNDArray As ArrayList = New ArrayList(10)
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            'create the array called al
            
            'fill the array with values 1-10
            For c = 1 To 10
                RNDArray.Add(c)
                RNDArray.TrimToSize()
                'display the contents of the array
                Response.Write(RNDArray.Item(c - 1))
            Next
        End If
        
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Non Repeating Random Numbers 1 - 10 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Choose Random 1-10" />
    
    </div>
    </form>
</body>
</html>

Open in new window

I took your code and played a bit with it. This is the result for now, it shuffles the numbers a bit around:

<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    
	Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
		Dim newArray As ArrayList = al.Clone()
		
		'select a random number between 1 and size of array
		Dim RNDNUM As New Random
		
		For i As Integer = 0 To al.Count - 1
			Dim myrandindex As Integer = RNDNUM.Next(1, newArray.Count)
			newArray(myrandindex) = al(i)
		Next
 
        
		'display the new contents of the array
		For idx = 0 To newArray.Count - 1
			Response.Write(newArray.Item(idx) & "-")
		Next
        
 
	End Sub
 
	Dim al As ArrayList = New ArrayList(10)
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            
		'fill the array with values 1-10
		For idx = 0 To 9
			al.Add(idx + 1)
			If Not IsPostBack Then
				Response.Write(al.Item(idx) & "-")
			End If
		Next
        
	End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Non Repeating Random Numbers 1 - 10 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Choose Random 1-10" />
    
    </div>
    </form>
</body>
</html>

Open in new window

> Its now flipping out at line 14 with 'minValue' cannot be greater than maxValue. Parameter name: minValue
yes, I've seen that error too. It is solved now.
wow that was fast. Let me just check the page to see if I understand it.

back in 5 (maybe 10) mins
ok.. I can see your code works but it doesnt really do what I am after.  Let me show you the output Im after (abel I might just not be understanding what you are showing me in your code) -

If you run the attached code which kind of works. you can see that i am choosing a random number and then removing it from the array. When I next press the button i want to again choose from the reduced array.

My problem is now that the array gets filled up 1-10 every time the page reloads.


<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    
	Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim newArray As ArrayList = al.Clone()
		
		'select a random number between 1 and size of array
		Dim RNDNUM As New Random
        Dim myrandindex As Integer = RNDNUM.Next(1, al.Count)
		
        'get the value from the array based up the random index position
        Dim myrandomvalue = al.Item(myrandindex - 1)
        
        'display the selected value of the array
        Response.Write("random number =")
        Response.Write(myrandomvalue)
        Response.Write("<br />")
        
        'remove the selected random number value from the array
        al.Remove(myrandindex)
        
        
		'display the new contents of the array
        For idx = 0 To al.Count - 1
            Response.Write(al.Item(idx) & "-")
        Next
        
 
	End Sub
 
	Dim al As ArrayList = New ArrayList(10)
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            
		'fill the array with values 1-10
		For idx = 0 To 9
			al.Add(idx + 1)
			If Not IsPostBack Then
				Response.Write(al.Item(idx) & " - ")
			End If
		Next
        
	End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Non Repeating Random Numbers 1 - 10 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Choose Random 1-10" />
    
    </div>
    </form>
</body>
</html>
 

Open in new window

> My problem is now that the array gets filled up 1-10 every time the page reloads.

yes, that's my fault, because I had to change your code to accomodate my slightly wrong understanding of the question. In fact, this is easier. Hold on.
SOLUTION
Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland 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
ASKER CERTIFIED 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
Thats Perfect.  

I didnt even know that it was possible to store an array as a session! And here's my clumsy reset at the end of the array, attached.

Thanks for all your help  

(Thanks to you too Jamie for being interested - you were right about the session)

R_C
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim al As ArrayList = DirectCast(Session("randomarray"), ArrayList)
        
        If al.Count = 1 Then
            'if the array is empty then fill it up with values 1-10
            For idx = 0 To 9
                al.Add(idx + 1)
                'Response.Write(al.Item(idx) & "-")
            Next
            Session("randomarray") = al
        End If
              
     
        'select a random number between 1 and size of array
        Dim RNDNUM As New Random
        
        
        Dim myrandindex As Integer = RNDNUM.Next(1, al.Count)
        Response.Write(al.Item(myrandindex))
        Response.Write("<br />")
        al.RemoveAt(myrandindex)
               
        
        'display the new contents of the array
        For idx = 0 To al.Count - 1
            Response.Write(al.Item(idx) & "-")
        Next
        
        ' place the results back
        Session("randomarray") = al
 
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            Dim al As ArrayList = New ArrayList(10)
        
            'fill the array with values 1-10
            For idx = 0 To 9
                al.Add(idx + 1)
                Response.Write(al.Item(idx) & "-")
            Next
            Session("randomarray") = al
        End If
        
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Non Repeating Random Numbers 1 - 10 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Choose Random 1-10" />
    
    </div>
    </form>
</body>
</html>
 

Open in new window

> Neat Solution and really really quick response.

thanks for a nice compliment, always there to help :)

Btw: are you aware of the possibility of splitting points? Other experts have also giving insightful information. With splitting points you can assign an amount of the points to each of the experts.
You know, I thought about that but I dont know how to split points :(
click the "Accept multiple solutions" link. But that's only available *before* you grade :)
If you still want to do that, you can send a request to the moderators by clicking "request attention" and asking for the question to be unlocked/reopened so that you can reassign points.
Ok I have requested an unlock and will split fairly.
Thanks for mentioning that Abel, you're a Gent.
You're welcome, Jamie. All in all, we are here to help fairly, so we should treat each other fairly, and help askers to find the ways to grade us well. ;-)

@Real_coffee: it may take a while until a moderator attends to the question, but normally it's somewhere within 12 hours.
Thanks once again.
lol, indeed excellent timing, WhackAMod!

actually, this brings me further away from Wizard status in this zone, and I was so close.. ;-). Tell me again, why was I doing this? :D