Modify naive search algo to allow tollerance - vb.net

I'm trying to mod the following naive search function, to find sequences that are in a range of tollerance (each digit can be +/- X%).
this is where I start:
Public Shared Function searchX(ByVal haystack As Byte(), ByVal needle As Byte())
Dim M As Integer = needle.Length
Dim N As Integer = haystack.Length
For i As Integer = 0 To N - M
Dim j As Integer
For j = 0 To M - 1
If haystack(i + j) <> needle(j) Then Exit For
Next
If j = M Then
Return i
End If
Next
End Function
So, if I correctly understand how the Naive algo work, the comparation of the pattern with my array is done in the line If haystack(i + j) <> needle(j) Then Exit For.
So I change it like that:
'code deleted as asked from my team that own it
I tested it with different array and patterns, but now it look like it can ony find a sequence of "0"; What am I missing? Is my understand of this algo totally wrong?
UPDATE:
I put some data to minimal testing:
Dim haystack = New Byte() {&H0, &H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &HA, &HB, &HC, &HD, &HE, &HF, &H1, &HF1, &HF4, &H3F, &H24, &HF4, &HF2, &HF4, &HF2, &H4F, &H2F, &H4F, &H2F, &H43, &HF2, &H4F, &H2F, &H4F, &H23, &H4F, &H23, &HF4, &HFF, &H42, &HF4, &HF3, &HF4, &H32, &HF4, &HF3, &H2F, &H4F, &H23, &HF0}
Dim haystack2 = New Byte() {&H0, &H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &HC, &HD, &HE, &HD, &HE, &HF, &H1, &HF1, &HF4, &H3F, &H24, &HF4, &HF2, &HF4, &HF2, &H4F, &H2F, &H4F, &H2F, &H43, &HF2, &H4F, &H2F, &H4F, &H23, &H4F, &H23, &HF4, &HFF, &H42, &HF4, &HF3, &HF4, &H32, &HF4, &HF3, &H2F, &H4F, &H23, &HF0}
Dim needle = New Byte() {&HA, &HB, &HC}
Dim tollerance = 20
Using the needle A,B,C in the first haystack and tolerance=0 I should get the match at position 10, and none in the hatstack2. Then with tollerance = 20 I should find a match in both haystack at the same position 10 because the needle's values still in the 20% of tollerance.

See my comments on the For loops.
Private haystack As Byte() = New Byte(){&H0, &H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &HA, &HB, &HC, &HD, &HE, &HF, &H1, &HF1, &HF4, &H3F, &H24, &HF4, &HF2, &HF4, &HF2, &H4F, &H2F, &H4F, &H2F, &H43, &HF2, &H4F, &H2F, &H4F, &H23, &H4F, &H23, &HF4, &HFF, &H42, &HF4, &HF3, &HF4, &H32, &HF4, &HF3, &H2F, &H4F, &H23, &HF0}
Private needle As Byte() = New Byte() {&HA, &HB, &HC}
Private haystack2 As Byte() = New Byte() {&H0, &H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &HC, &HD, &HE, &HD, &HE, &HF, &H1, &HF1, &HF4, &H3F, &H24, &HF4, &HF2, &HF4, &HF2, &H4F, &H2F, &H4F, &H2F, &H43, &HF2, &H4F, &H2F, &H4F, &H23, &H4F, &H23, &HF4, &HFF, &H42, &HF4, &HF3, &HF4, &H32, &HF4, &HF3, &H2F, &H4F, &H23, &HF0}
Public Shared Function searchXWithT(ByVal haystack As Byte(), ByVal needle As Byte(), ByVal err As Byte) As Integer
Dim M As Integer = needle.Length
Dim N As Integer = haystack.Length
For i As Integer = 0 To N - M 'For i = 0 To 10
For j = 0 To M - 1 'For j = 0 To 2
If (CDbl(needle(j) - (needle(j) / 100 * err)) < CDbl(haystack(i + j))) And (CDbl(haystack(i + j)) < CDbl((needle(j) / 100 * err) + needle(j))) Then
Else
Exit For
End If
Next
If j = M Then 'M will always be 3 and j can never be more than 2
Return i 'i will never be returned
End If
Next
Return 0
End Function

Related

How to print listview table in vb.net?

Hi, I need to print my listview, but I'm not sure how to do it?
Public Class listViewPrinter
Private lv As ListView
Private location As Point
Private border As Boolean
Private title As String
Private titleHeight As Integer
Private hLines As Boolean
Private vLines As Boolean
Private WithEvents pd As New Printing.PrintDocument
Public Sub New(ByVal lv As ListView, ByVal location As Point, ByVal border As Boolean, ByVal _hLines As Boolean, ByVal _vLines As Boolean, Optional ByVal title As String = "")
Me.lv = lv
Me.location = location
Me.border = border
Me.title = title
Me.hLines = _hLines
Me.vLines = _vLines
titleHeight = If(title <> "", lv.FindForm.CreateGraphics.MeasureString(title, New Font(lv.Font.Name, 25)).ToSize.Height, 0)
End Sub
Public Sub print()
'pd.Print()
Dim ppd As New PrintPreviewDialog
ppd.Document = pd
ppd.WindowState = FormWindowState.Maximized
ppd.ShowDialog()
End Sub
Private Structure pageDetails ' structure to hold printed page details
Dim columns As Integer
Dim rows As Integer
Dim startCol As Integer
Dim startRow As Integer
Dim headerIndices As List(Of Integer)
End Structure
Private pages As Dictionary(Of Integer, pageDetails) ' dictionary to hold printed page details, with index key
Dim maxPagesWide As Integer
Dim maxPagesTall As Integer
Dim items() As ListViewItem
' the majority of this Sub is calculating printed page ranges
Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.BeginPrint
pd.DefaultPageSettings.Landscape = True
pd.OriginAtMargins = True 'this removes the printed page margins
pd.DefaultPageSettings.Margins = New Drawing.Printing.Margins(location.X, location.X, location.Y, location.Y)
pages = New Dictionary(Of Integer, pageDetails)
' Reversed MaxWidth & MaxHeight for landscape page...
'Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width) - (pd.DefaultPageSettings.Margins.Left + pd.DefaultPageSettings.Margins.Right + 40)
'Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height - (titleHeight + 12)) - (pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom + 40)
Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height) - (pd.DefaultPageSettings.Margins.Left + pd.DefaultPageSettings.Margins.Right + 0)
Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width - (titleHeight + 12)) - (pd.DefaultPageSettings.Margins.Top + pd.DefaultPageSettings.Margins.Bottom + 0)
Dim pageCounter As Integer = 0
pages.Add(pageCounter, New pageDetails With {.headerIndices = New List(Of Integer)})
Dim columnCounter As Integer = 0
Dim columnSum As Integer = 0
For c As Integer = 0 To lv.Columns.Count - 1
If columnSum + lv.Columns(c).Width < maxWidth Then
columnSum += lv.Columns(c).Width
columnCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
columnSum = lv.Columns(c).Width
columnCounter = 1
pageCounter += 1
pages.Add(pageCounter, New pageDetails With {.startCol = c, .headerIndices = New List(Of Integer)})
End If
If c = lv.Columns.Count - 1 Then
If pages(pageCounter).columns = 0 Then
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
End If
End If
Next
maxPagesWide = pages.Keys.Max + 1
pageCounter = 0
Dim rowCounter As Integer = 0
Dim counter As Integer = 0
Dim itemHeight As Integer = lv.GetItemRect(0).Height
Dim rowSum As Integer = itemHeight
For r As Integer = 0 To lv.Items.Count - 1
counter += 1
If rowSum + itemHeight < maxHeight Then
rowSum += itemHeight
rowCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
For x As Integer = 1 To maxPagesWide - 1
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
Next
pageCounter += maxPagesWide
For x As Integer = 0 To maxPagesWide - 1
pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = counter - 1})
Next
rowSum = itemHeight * 2
rowCounter = 1
End If
If counter = lv.Items.Count Then
For x As Integer = 0 To maxPagesWide - 1
If pages(pageCounter + x).rows = 0 Then
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
End If
Next
End If
Next
maxPagesTall = pages.Count \ maxPagesWide
items = lv.Items.Cast(Of ListViewItem).ToArray
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Dim r2 As New Rectangle(location, New Size(lv.Columns.Cast(Of ColumnHeader).Skip(pages(0).startCol).Take(pages(0).columns).Sum(Function(ch As ColumnHeader) ch.Width), titleHeight))
e.Graphics.DrawString(title, New Font(lv.Font.Name, 25), Brushes.Black, r2, sf)
sf.Alignment = StringAlignment.Near
Dim startX As Integer = location.X
Dim startY As Integer = location.Y + titleHeight + 12
Static startPage As Integer = 0
Dim itemHeight As Integer = lv.GetItemRect(0).Height
Dim bottomRight As Point
For p As Integer = startPage To pages.Count - 1
startX = location.X
startY = location.Y + titleHeight + 12
Dim cell As Rectangle
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(lv.Columns(c).Text, lv.Font, Brushes.Black, cell, sf)
startX += lv.Columns(c).Width
Next
startY += itemHeight
startX = location.X
For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
startX = location.X
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
e.Graphics.DrawString(items(r).SubItems(c).Text, lv.Font, Brushes.Black, cell, sf)
If vLines Then
e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Top, cell.Left, cell.Bottom)
e.Graphics.DrawLine(Pens.Black, cell.Right, cell.Top, cell.Right, cell.Bottom)
End If
If hLines Then
e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Top, cell.Right, cell.Top)
e.Graphics.DrawLine(Pens.Black, cell.Left, cell.Bottom, cell.Right, cell.Bottom)
End If
startX += lv.Columns(c).Width
Next
startY += itemHeight
If r = pages(p).startRow + pages(p).rows - 1 Then
bottomRight = New Point(startX, startY)
If border Then e.Graphics.DrawRectangle(Pens.Black, New Rectangle(location, New Size(bottomRight.X - location.X, bottomRight.Y - location.Y)))
End If
Next
If p <> pages.Count - 1 Then
startPage = p + 1
e.HasMorePages = True
Return
Else
startPage = 0
End If
Next
End Sub
End Class
Courtesy of

How do I create a function or procedure for this kind of separate functions

Public Sub CheckTwo(ByVal str As String)
If ((arrayTwo(colArray(0), colArray(0)) = str) And (arrayTwo(colArray(1), colArray(1)) = str)) Or
((arrayTwo(colArray(0), colArray(1)) = str) And (arrayTwo(colArray(1), colArray(0)) = str)) Then
MsgBox(str + " Won")
End If
For i = 0 To colArray.Length - 1
If (arrayTwo(colArray(i), colArray(0)) = str) And (arrayTwo(colArray(i), colArray(1)) = str) Or
(arrayTwo(colArray(0), colArray(i)) = str) And (arrayTwo(colArray(1), colArray(i)) = str) Then
MsgBox(str + " Won")
End If
Next
End Sub
Public Sub CheckThree(ByVal str As String)
If ((arrayTwo(colArray(0), colArray(0)) = str) And (arrayTwo(colArray(1), colArray(1)) = str) And
(arrayTwo(colArray(2), colArray(2)) = str)) Or
((arrayTwo(colArray(0), colArray(2)) = str) And (arrayTwo(colArray(1), colArray(1)) = str) And
(arrayTwo(colArray(2), colArray(0)) = str)) Then
MsgBox(str + " Won")
End If
For i = 0 To colArray.Length - 1
If ((arrayTwo(colArray(0), colArray(i)) = str) And (arrayTwo(colArray(1), colArray(i)) = str) And
(arrayTwo(colArray(2), colArray(i)) = str)) Or
((arrayTwo(colArray(i), colArray(0)) = str) And (arrayTwo(colArray(i), colArray(1)) = str) And
(arrayTwo(colArray(i), colArray(2)) = str)) Then
MsgBox(str + " Won")
End If
Next
End Sub
Public Sub CheckFour(ByVal str As String)
If ((arrayTwo(colArray(0), colArray(0)) = str) And (arrayTwo(colArray(1), colArray(1)) = str) And
(arrayTwo(colArray(2), colArray(2)) = str) And (arrayTwo(colArray(3), colArray(3)) = str)) Or
((arrayTwo(colArray(3), colArray(0)) = str) And (arrayTwo(colArray(2), colArray(1)) = str) And
(arrayTwo(colArray(1), colArray(2)) = str) And (arrayTwo(colArray(0), colArray(3)) = str)) Then
MsgBox(str + " Won")
End If
For i = 0 To colArray.Length - 1
If ((arrayTwo(colArray(0), colArray(i)) = str) And (arrayTwo(colArray(1), colArray(i)) = str) And
(arrayTwo(colArray(2), colArray(i)) = str) And (arrayTwo(colArray(3), colArray(i)) = str)) Or
((arrayTwo(colArray(i), colArray(0)) = str) And (arrayTwo(colArray(i), colArray(1)) = str) And
(arrayTwo(colArray(i), colArray(2)) = str) And (arrayTwo(colArray(i), colArray(3)) = str)) Then
MsgBox(str + " Won")
End If
Next
End Sub
And so on up to dynamically enter value for colArray(x,x)
It might help if we knew the entirety of what you were trying to accomplish.
In any case, you may consider something along these lines:
Public Sub CheckN(ByVal str As String, ByVal CheckCount As Integer)
For i = 0 To CheckCount - 1
Dim TruthList As New List(Of Boolean)
For j = 0 To colArray.Length - 1
If arrayTwo(colArray(i), colArray(j)) = str Then
TruthList.Add(True)
End If
Next
If TruthList.Distinct.Count < 2 Then
MsgBox(str + " Won")
End If
Next
End Sub

Reduce latency when Embossing an Image in PictureBox

How would I go about reducing the latency of grabbing an image from Picturebox then emboss that image and return you picturebox as this is a camera handle and will need to capture/emboss at least 16 fps but i'm getting like 1 every 2.5 seconds
SwitchImageSave = 1
Button1.Enabled = False
StCam.StopTransfer(m_hCamera)
Dim nReval As Integer
Dim nLastErrorNo As Integer
Dim nBufferSize As Integer
Dim dwWidth As Integer
Dim dwHeight As Integer
Dim dwLinePitch As Integer
nReval = StCam.GetPreviewDataSize(m_hCamera, nBufferSize, dwWidth, dwHeight, dwLinePitch)
Dim dwPreviewPixelFormat As Integer
nReval = StCam.GetPreviewPixelFormat(m_hCamera, dwPreviewPixelFormat)
Dim pixelFormat As Imaging.PixelFormat = Imaging.PixelFormat.Format8bppIndexed
Select Case dwPreviewPixelFormat
Case StCam.STCAM_PIXEL_FORMAT_24_BGR
pixelFormat = Imaging.PixelFormat.Format24bppRgb
Case StCam.STCAM_PIXEL_FORMAT_32_BGR
pixelFormat = Imaging.PixelFormat.Format32bppRgb
End Select
Dim pbyteImageBuffer(nBufferSize) As Byte
Dim dwNumberOfByteTrans As Integer = 0
Dim pdwFrameNo(1) As Integer
Dim dwMilliseconds As Integer = 100
Dim gch As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(pbyteImageBuffer, System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ptr As IntPtr = gch.AddrOfPinnedObject()
nReval = StCam.TakePreviewSnapShot(m_hCamera, ptr, nBufferSize, dwNumberOfByteTrans, pdwFrameNo, dwMilliseconds)
gch.Free()
Dim bitmap As Bitmap = New Bitmap(dwWidth, dwHeight, pixelFormat)
Select Case pixelFormat
Case Imaging.PixelFormat.Format8bppIndexed
Dim colorPalette As System.Drawing.Imaging.ColorPalette = bitmap.Palette
For pixelValue As Integer = 0 To 255
colorPalette.Entries(pixelValue) = Color.FromArgb(pixelValue, pixelValue, pixelValue)
Next
bitmap.Palette = colorPalette
End Select
Dim bitmapData As Imaging.BitmapData = bitmap.LockBits(New Rectangle(0, 0, dwWidth, dwHeight), Imaging.ImageLockMode.WriteOnly, pixelFormat)
Runtime.InteropServices.Marshal.Copy(pbyteImageBuffer, 0, bitmapData.Scan0(), nBufferSize)
bitmap.UnlockBits(bitmapData)
Dim bmap As Bitmap
bmap = New Bitmap(bitmap)
PictureBox2.Image = bmap
' PictureBox2.Image = bmap
Dim tempbmp As New Bitmap(bmap)
Dim i, j As Integer
Dim DispX As Integer = 1, DispY As Integer = 1
Dim red, green, blue As Integer
With tempbmp
For i = 0 To .Height - 2
For j = 0 To .Width - 2
Dim pixel1, pixel2 As System.Drawing.Color
pixel1 = .GetPixel(j, i)
pixel2 = .GetPixel(j + DispX, i + DispY)
red = Math.Min(Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) + 128, 255)
green = Math.Min(Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) + 128, 255)
blue = Math.Min(Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) + 128, 255)
bmap.SetPixel(j, i, Color.FromArgb(red, green, blue))
Next
Next
End With
PictureBox2.Image = bmap
PictureBox2.Refresh()
PictureBox2.BringToFront()
Dim bitmapData As Imaging.BitmapData = bitmap.LockBits(New Rectangle(0, 0, dwWidth, dwHeight), Imaging.ImageLockMode.WriteOnly, pixelFormat)
Runtime.InteropServices.Marshal.Copy(pbyteImageBuffer, 0, bitmapData.Scan0(), nBufferSize)
bitmap.UnlockBits(bitmapData)
PictureBox2.Image = bitmap
Dim temp As Bitmap = PictureBox2.Image
Dim raz As Integer = temp.Height / 4
Dim height As Integer = temp.Height
Dim width As Integer = temp.Width
Dim rect As New Rectangle(Point.Empty, temp.Size)
Dim bmpData As BitmapData = temp.LockBits(rect, ImageLockMode.[ReadOnly], temp.PixelFormat)
Dim bpp As Integer = If((temp.PixelFormat = PixelFormat.Format32bppArgb), 4, 3)
Dim size As Integer = bmpData.Stride * bmpData.Height
Dim data As Byte() = New Byte(size - 1) {}
'byte[] newdata = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size)
'System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, newdata, 0, size);
Dim options = New ParallelOptions()
Dim maxCore As Integer = Environment.ProcessorCount - 1
options.MaxDegreeOfParallelism = If(maxCore > 0, maxCore, 1)
For y As Integer = 0 To height - 4
For x As Integer = 0 To width - 4
If True Then
Dim index As Integer = y * bmpData.Stride + x * bpp
'Blue
data(index) = CByte(Math.Min(Math.Abs(CInt(data(index)) - CInt(data(index + bpp + bmpData.Stride))) + 128, 255))
'Green
data(index + 1) = CByte(Math.Min(Math.Abs(CInt(data(index + 1)) - CInt(data(index + bpp + 1 + bmpData.Stride))) + 128, 255))
'Red
data(index + 2) = CByte(Math.Min(Math.Abs(CInt(data(index + 2)) - CInt(data(index + bpp + 2 + bmpData.Stride))) + 128, 255))
End If
Next
Next

How to make a random not deplicate with another random number VB.NET

I'm trying to make the content of a button be a random number each time the user opens the application. Here is the code so far.
Dim rndnumber As Random
Dim number1 As Integer
Dim number2 As Integer
Dim number3 As Integer
Dim number4 As Integer
Dim number5 As Integer
Dim number6 As Integer
Dim number7 As Integer
Dim number8 As Integer
Dim number9 As Integer
Dim number10 As Integer
Dim number11 As Integer
Dim number12 As Integer
Dim number13 As Integer
Dim number14 As Integer
Dim number15 As Integer
Dim number16 As Integer
Dim number17 As Integer
rndnumber = New Random
Do
number1 = rndnumber.Next(0, 18)
number2 = rndnumber.Next(0, 18)
number3 = rndnumber.Next(0, 18)
number4 = rndnumber.Next(0, 18)
number5 = rndnumber.Next(0, 18)
number6 = rndnumber.Next(0, 18)
number7 = rndnumber.Next(0, 18)
number8 = rndnumber.Next(0, 18)
number9 = rndnumber.Next(0, 18)
number10 = rndnumber.Next(0, 18)
number11 = rndnumber.Next(0, 18)
number12 = rndnumber.Next(0, 18)
number13 = rndnumber.Next(0, 18)
number14 = rndnumber.Next(0, 18)
number15 = rndnumber.Next(0, 18)
number16 = rndnumber.Next(0, 18)
number17 = rndnumber.Next(0, 18)
Loop Until number1 IsNot number2 Or number3 Or number4 Or number5 Or number6 Or number7 Or number8 Or number9 Or number10 Or number11 Or number12 Or number13 Or number14 Or number15 Or number16 Or number17
button1.Content = number1.ToString
button2.Content = number2.ToString
button3.Content = number3.ToString
button4.Content = number4.ToString
button5.Content = number5.ToString
button6.Content = number6.ToString
button7.Content = number7.ToString
button8.Content = number8.ToString
button9.Content = number9.ToString
button10.Content = number10.ToString
button11.Content = number11.ToString
button12.Content = number12.ToString
button13.Content = number13.ToString
button14.Content = number14.ToString
button15.Content = number15.ToString
button16.Content = number16.ToString
button17.Content = number17.ToString
Here is the first of 17 buttons, i have the same code for each button but have chanaged the "loop until number1 isnot number 2" part to match each different number.
The problem is at the "Isnot" part at the loop.
How can i get it to understand the value of each rndnumber?
Thanks in advanced.
You could add each random number to a list, then in the Loop While condition add something like: rndList.Count <> rndList.Distinct.ToList.Count.
Here's a quick 1 AM go at it:
Dim rndnumber As Random = New Random
Dim rndList As List(Of Integer)
Do
rndList = New List(Of Integer)
For i = 1 To 17
rndList.Add(rndnumber.Next(0, 18))
Next
Loop While rndList.Count <> rndList.Distinct.ToList.Count
button1.Content = rndList(0).ToString
button2.Content = rndList(1).ToString
button3.Content = rndList(2).ToString
button4.Content = rndList(3).ToString
button5.Content = rndList(4).ToString
button6.Content = rndList(5).ToString
button7.Content = rndList(6).ToString
button8.Content = rndList(7).ToString
button9.Content = rndList(8).ToString
button10.Content = rndList(9).ToString
button11.Content = rndList(10).ToString
button12.Content = rndList(11).ToString
button13.Content = rndList(12).ToString
button14.Content = rndList(13).ToString
button15.Content = rndList(14).ToString
button16.Content = rndList(15).ToString
button17.Content = rndList(16).ToString
Note: I don't use windows-phone, so this answer is mostly relative to .Net in general and may be not applicable as-is
To work that way your code would have to have a giant condition with something like that (with variables shortened):
Loop While n1 <> n2 AndAlso n1 <> n3 ... AndAlso n1 <> n17 AndAlso n2 <> n3 ... AndAlso n16 <> n17
That's 16 + 15 + ... + 1 = 136 condition to write and fullfill ; There is a great chance you'll be stuck for a while in that loop.
A simpler approach is to construct an array with your values and then shuffle it; something like :
' suppose a Random instance named "rnd" created somewhere
Dim values = Enumerable.Range(0, 18).OrderBy(Function(unused) rnd.NextDouble).ToArray
button1.Content = values(0).ToString
' same for other button
Maybe it should also be possible to have some kind of loop to fill the values inside the buttons.
This approach assumes that your buttons are named as shown. It is a .Net Forms approach but might be modified to work with Phone.
Dim rndnumber As New Random
Dim butContent As List(Of Integer) = Enumerable.Range(1, 17).ToList
For x As Integer = 1 To 17
Dim idx As Integer = rndnumber.Next(butContent.Count)
Dim cntnt As String = butContent(idx).ToString
butContent.RemoveAt(idx)
Me.Controls("button" & x.ToString).Text = cntnt
Next

Unsigned Byte flip with *.bin file [duplicate]

This question already has answers here:
16 bit unsigned short byte flip in VB.NET
(2 answers)
Closed 8 years ago.
I need example in code, how to byte flip whole binary file. VB.NET
Example
02 00 0D 78 10 20 40 80 F1 F2 F4 F8 1F 2F 4F 8F
Flip Operation
00 02 78 0D 20 10 80 40 F2 F1 F8 F4 2F 1F 8F 4F
Whole, binary file using OpenFileDialog (OFD)
You just need to loop all items of the array and swap each bytes.
Dim bytes() As Byte = {&H2, &H0, &HD, &H78, &H10, &H20, &H40, &H80, &HF1, &HF2, &HF4, &HF8, &H1F, &H2F, &H4F, &H8F}
For i As Integer = 0 To bytes.Length - 1 Step 2
bytes(i) = bytes(i) Xor bytes(i + 1)
bytes(i + 1) = bytes(i + 1) Xor bytes(i)
bytes(i) = bytes(i) Xor bytes(i + 1)
Next
If you want, use a temp variable
For i As Integer = 0 To bytes.Length - 1 Step 2
Dim tempByte As Byte = bytes(i)
bytes(i) = bytes(i+1)
bytes(i + 1) = tempByte
Next
erm, you could write as you go, something like,
Dim file = dialog.FileName
Using output = New BinaryWriter(New FileStream( _
file, _
FileMode.Append, _
FileAccess.Write, _
FileShare.Read))
Using input = New BinaryReader(New FileStream( _
file, _
FileMode.Open, _
FileAccess.Read, _
FileShare.ReadWrite))
Dim bufferLength = 2
While bufferLength = 2
Dim buffer = input.ReadBytes(2)
bufferLength = buffer.Length
If bufferLength = 2 Then
output.Write(buffer(1))
output.Write(buffer(0))
Else If bufferLength = 1 Then
output.Write(buffer(0))
End If
End While
End Using
End Using