## 11 replies

Last post Sep 19, 2014 07:04 AM by skeeneyman

Member

27 Points

152 Posts

### Non Random repeating sequence of 8 integers

This may be easy to do but I can't seem to figure it out or how to phrase a search, so I figured I would describe what I'm looking for here and see what the pros say. What I need is a sequence of 8 integers within a range of 1 to 20 making the 1st sequence to be 1, 2, 3, 4, 5, 6, 7, 8 then I need the 2nd sequence to be 9, 10, 11, 12, 13, 14, 15, 16 then the 3rd sequence 17, 18, 19, 20, 1, 2, 3, 4 4th sequence 5, 6, 7, 8, 9, 10, 11, 12 and so on. each new sequence is triggered every time the sub routine is called, which in this case is every 60 seconds.

Currently I'm generating the range of numbers randomly, but I don't want them to be random.

sequence vb.net integers

skeeneyman
• ### Rion William...

All-Star

113110 Points

18367 Posts

MVP

### Re: Non Random repeating sequence of 8 integers

Sep 18, 2014 09:52 AM|Rion Williams|LINK

I suppose that you could use LINQ and the Enumerable.Range() method to generate a collection of n consecutive integers. You could combine this by passing in a starting value, a sequence size and a maximum integer value (in this case 20) which you can handle through some modulo arithmetic :

```public static IEnumerable<int> GenerateSet(int startingInteger, int sequenceSize, int maxValue, out int lastValue)
{
// Build your list (use a fix-up to ignore zeros)
var values = Enumerable.Range(startingInteger, sequenceSize).Select(x => (x % maxValue) + 1);

// Store your last value (as you'll output it to re-seed your sequence)
lastValue = values.LastOrDefault();

return values;
}```

A complete example might look like this :

```// Store your starting value, sequence size, max value
int start = 0; // Uses 0 because of 1 fix-up and initial seeding
int seqSize = 8;
int maxValue = 20;

var set1 = GenerateSet(start,seqSize,maxValue, out start);

// Output set1
Console.WriteLine(String.Join(",",set1.ToArray())); // yields "1,2,3,4,5,6,7,8"

// Generate another set
var set2 = GenerateSet(start,seqSize,maxValue, out start);
Console.WriteLine(String.Join(",",set2.ToArray())); // yields "9,10,11,12,13,14,15,16"

// Generate another set
var set3 = GenerateSet(start,seqSize,maxValue, out start);
Console.WriteLine(String.Join(",",set3.ToArray())); // yields "17,18,19,20,1,2,3,4"

// Generate another set
var set4 = GenerateSet(start,seqSize,maxValue, out start);
Console.WriteLine(String.Join(",",set4.ToArray())); // yields "5,6,7,8,9,10,11,12"```

You can see a working example here.

sequence vb.net integers

• ### Rion William...

All-Star

113110 Points

18367 Posts

MVP

### Re: Non Random repeating sequence of 8 integers

Sep 18, 2014 09:56 AM|Rion Williams|LINK

I just noticed that your question was asking specifically for Visual Basic, so here is the same code in Visual Basic :

```Public Sub Main()
Dim start As Integer = 0
Dim seqSize As Integer = 8
Dim maxValue As Integer = 20

' Generate your first set '
Dim set1 = GenerateSet(start, seqSize, maxValue, start)

' Output set1 '
Console.WriteLine(String.Join(",", set1.ToArray()))

' Generate another set '
Dim set2 = GenerateSet(start, seqSize, maxValue, start)
Console.WriteLine(String.Join(",", set2.ToArray()))

' Generate another set '
Dim set3 = GenerateSet(start, seqSize, maxValue, start)
Console.WriteLine(String.Join(",", set3.ToArray()))

' Generate another set '
Dim set4 = GenerateSet(start, seqSize, maxValue, start)
Console.WriteLine(String.Join(",", set4.ToArray()))
End Sub

Public Function GenerateSet(startingInteger As Integer, sequenceSize As Integer, maxValue As Integer, ByRef lastValue As Integer) As IEnumerable(Of Integer)
' Build your list (use a fix-up to ignore zeros) '
Dim values = Enumerable.Range(startingInteger, sequenceSize).Select(Function(x) (x Mod maxValue) + 1)

' Store your last value '
lastValue = values.LastOrDefault()

Return values
End Function```

You can find a working example in Visual Basic here.

sequence vb.net integers

Member

27 Points

152 Posts

### Re: Non Random repeating sequence of 8 integers

Rion! Thanks so much. I might have put this thread in the wrong forum. This function is for a Web Form application, so I'm not entirely sure how to translate it. Any suggestions?

sequence vb.net integers

skeeneyman
• ### Rion William...

All-Star

113110 Points

18367 Posts

MVP

### Re: Non Random repeating sequence of 8 integers

Sep 18, 2014 10:46 AM|Rion Williams|LINK

#### skeeneyman

Rion! Thanks so much. I might have put this thread in the wrong forum. This function is for a Web Form application, so I'm not entirely sure how to translate it. Any suggestions?

I was just using a Console application as an example to demonstrate the functionality. The general usage should be roughly the same as you can see in this Example Web Forms application below :

Example.aspx

```<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Example.aspx.vb" Inherits="WebApplication1.Example" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<title></title>
<body>
<form id="form1" runat="server">
<!-- A ListBox to store your sequences -->
<asp:ListBox ID="YourSequences" runat="server"></asp:ListBox>
<asp:Button ID="GenerateSequence" runat="server" Text="Add Sequence" />
</form>
</body>
</html>```

Example.aspx.vb

```Public Class Example
Inherits System.Web.UI.Page

' Define static variables that can be reused '
Public Shared Start As Integer = 0
Public SeqSize As Integer = 8
Public MaxValue As Integer = 20

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub GenerateSequence_Click(sender As Object, e As EventArgs) Handles GenerateSequence.Click
Dim sequence = GenerateSet(Start, SeqSize, MaxValue, Start)
End Sub

Public Function GenerateSet(startingInteger As Integer, sequenceSize As Integer, maxValue As Integer, ByRef lastValue As Integer) As IEnumerable(Of Integer)
' Build your list (use a fix-up to ignore zeros) '
Dim values = Enumerable.Range(startingInteger, sequenceSize).Select(Function(x) (x Mod maxValue) + 1)
' Store your last value '
lastValue = values.LastOrDefault()
Return values
End Function
End Class```

which after a few clicks would yield :

sequence vb.net integers

Member

27 Points

152 Posts

### Re: Non Random repeating sequence of 8 integers

Never mind I got it.

So how would the function go from one set to the next after a postback?

sequence vb.net integers

skeeneyman

Member

27 Points

152 Posts

### Re: Non Random repeating sequence of 8 integers

That's Brilliant! OK, let me put it together in my application and I'll mark this as complete.

sequence vb.net integers

skeeneyman
• ### Rion William...

All-Star

113110 Points

18367 Posts

MVP

### Re: Non Random repeating sequence of 8 integers

Sep 18, 2014 11:04 AM|Rion Williams|LINK

#### skeeneyman

So how would the function go from one set to the next after a postback?

That's handled through the Shared variables (which are the equivalent of C#'s static).

sequence vb.net integers

Member

27 Points

152 Posts

### Re: Non Random repeating sequence of 8 integers

Crap, sorry I can't seem to figure out how to adapt this to my code. Let me post my code and explain what I'm trying to do.

Basically, I have 8 images in my application that need to be a different set of images depending on where the webpage is being viewed (based on the name of the computer) and they need to change every time there is a post back. There can be an unlimited number of potential images for the webpage so loop through. (does that make any sense?) Currently I'm selecting the images randomly.

Here is my code

Sub ImageGen()
Try
Dim ImageLocation As String = ""
Dim Images() As Image = New Image() {Image1, Image2, Image3, Image4, Image5, Image6, Image7, Image8}
Dim RemeberSet As New HashSet(Of Integer)
Dim numbers = Enumerable.Range(0, 20).ToList()
Dim RandomClass As New Random()
Dim RandomIndex As Integer
'Images = New Image() {Image1, Image2, Image3, Image4}
For index = 0 To Images.GetUpperBound(0)
'For counter As Integer = 1 To 5
Select Case Session("SiteID")
Case "67"
ImageLocation = "~/img/Banneker/slide"
RandomIndex = RandomClass.Next(0, 10)
Images(index).ImageUrl = ImageLocation & RandomIndex & ".jpg"
Body.Attributes("class") = "banneker"
Case "79"
ImageLocation = "~/img/Tri-Cities/slide"
RandomIndex = RandomClass.Next(1, 13)
Images(index).ImageUrl = ImageLocation & RandomIndex & ".jpg"
Body.Attributes("class") = "tri-cities"
Case "229"
ImageLocation = "~/img/Cambridge/slide"
RandomIndex = RandomClass.Next(0, 10)
Images(index).ImageUrl = ImageLocation & RandomIndex & ".jpg"
Body.Attributes("class") = "cambridge"
Case "69"
ImageLocation = "~/img/Chattahoochee/slide"
RandomIndex = RandomClass.Next(0, 10)
Images(index).ImageUrl = ImageLocation & RandomIndex & ".jpg"
Body.Attributes("class") = "chattahoochee"
Case "70"
ImageLocation = "~/img/Creekside/slide"
RandomIndex = RandomClass.Next(0, 11)
Images(index).ImageUrl = ImageLocation & RandomIndex & ".jpg"
Body.Attributes("class") = "creekside"
Case "78"
ImageLocation = "~/img/RoswellHS/slide"
RandomIndex = RandomClass.Next(0, 10)
Images(index).ImageUrl = ImageLocation & RandomIndex & ".jpg"
Body.Attributes("class") = "roswellhs"
End Select
'Next
'End While
numbers.RemoveAt(RandomIndex)
Next
Catch ex As Exception
ErrorLb.Text = ex.Message & " Image Gen"
End Try
End Sub

sequence vb.net integers

skeeneyman

Member

27 Points

152 Posts

### Re: Non Random repeating sequence of 8 integers

It may not be obvious, but each potential image is named Slide1, Slide2, Slide3 and number I'm generating is being used determine the Imagepath. So in the code it's more like Slide & sequence, Slide & sequence, Slide & sequence

I don't know if I'm explaining this very well...sorry

sequence vb.net integers

skeeneyman

Member

27 Points

152 Posts

### Re: Non Random repeating sequence of 8 integers

sequence vb.net integers

skeeneyman

Member

27 Points

152 Posts