-
Dec 12th, 2010, 05:15 PM
#1
Thread Starter
Lively Member
random characters
Okay, so below we have a string:
`1234567890-=~!@#$%^&*()_+qwertyuiop[]\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:"zxcvbnm,./ZXCVBNM<>?
For each character of this string I want a new character out of the string and then remove the character from the list of characters that still maybe used for other characters. It may not get the same character, you could basically just call this encryption, but it's not what I am making. I don't want to waste my time doing this one hour while VB can do this for me in <1 second.
So any code would be very appreciated!
-
Dec 12th, 2010, 05:24 PM
#2
Re: random characters
A hint
Code:
Dim prng As New Random
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim allowedCH As New System.Text.StringBuilder("`1234567890-=~!@#$%^&*()_+qwertyuiop[]\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:""zxcvbnm,./ZXCVBNM<>?")
Dim newRandCharString As New System.Text.StringBuilder
Stop
'press F8 and use the debugger to gain understanding
Do While allowedCH.Length > 0
Dim idx As Integer = prng.Next(allowedCH.Length)
newRandCharString.Append(allowedCH(idx))
allowedCH.Remove(idx, 1)
Loop
Debug.WriteLine(newRandCharString.ToString)
End Sub
Last edited by dbasnett; Dec 12th, 2010 at 08:26 PM.
-
Dec 12th, 2010, 05:50 PM
#3
Thread Starter
Lively Member
Re: random characters
I am sorry to say this, but I have no idea what you are doing with the System.Text.StringBuilders.
The only thing I understand about it is: when allowed characters > 0 then it will 'Append' a StringBuilder.
I'd like some more information about this code, and if possible the full code. Thanks in advance!
-
Dec 12th, 2010, 08:25 PM
#4
Re: random characters
Originally Posted by Teunjack
I am sorry to say this, but I have no idea what you are doing with the System.Text.StringBuilders.
The only thing I understand about it is: when allowed characters > 0 then it will 'Append' a StringBuilder.
I'd like some more information about this code, and if possible the full code. Thanks in advance!
I edited my previous post so you could test the code. Here is a link to the documentation: http://msdn.microsoft.com/en-us/libr...ngbuilder.aspx
-
Dec 13th, 2010, 02:15 PM
#5
Thread Starter
Lively Member
Re: random characters
Sorry, but i'm having an error at the following line:
vb.net Code:
Dim idx As Integer = prng.Next(allowedCH.Length)
Error: System.NullReferenceException was unhandled. Object reference not set to an instance of an object.
I think i'll have to explain what I want better: I want to pick it a new char for the current char, and note the current char in RichTextBox1 and the new char after it, and after that add a VbCrlf/VbNewLine, so you'll get like some kind of 'encryption alphabet'.
I would greatly appreciate this code!
-
Dec 13th, 2010, 02:36 PM
#6
Re: random characters
@dbas, what's "prng" in the code you posted? I can't find a declaration for it.
-
Dec 13th, 2010, 03:18 PM
#7
Re: random characters
It's a random object, and the declaration is the very first line of his post.
@Teunjack: Did you get that "Private prng as New Random" line as DBas has?
@DBas: F8 is not going to work for all configurations of VS. For many of us, it is F10 and F11 that are used, instead.
My usual boring signature: Nothing
-
Dec 13th, 2010, 03:42 PM
#8
Re: random characters
ah, there it is. the browser scrolled down the "code block" and it was hidden.
-
Dec 13th, 2010, 04:24 PM
#9
Re: random characters
Originally Posted by Shaggy Hiker
It's a random object, and the declaration is the very first line of his post.
@Teunjack: Did you get that "Private prng as New Random" line as DBas has?
@DBas: F8 is not going to work for all configurations of VS. For many of us, it is F10 and F11 that are used, instead.
Did not know that shag, thanks.
-
Dec 13th, 2010, 05:39 PM
#10
Re: random characters
Yet ANOTHER randomise question... at least this one has the decency to provide a different datatype than Integers
I'm also sensing a request to be able to pull characters from the randomised source, rather than just randomising the whole thing... since VB doesn't have iterator blocks yet, I did a poor man's Enumerable (I can't be faffed implementing IEnumerable and IEnumerator, so this'll do)
vbnet Code:
Public Class RandomCharacters
Private ReadOnly m_allowedCharacters() As Char
Private m_remainingCharacters As Integer
Private ReadOnly m_random As System.Random
Public Sub New(ByVal allowedCharacters As String, ByRef random As System.Random)
m_allowedCharacters = allowedCharacters.ToCharArray
m_remainingCharacters = m_allowedCharacters.Length
m_random = random
End Sub
Public ReadOnly Property CharactersRemaining As Boolean
Get
Return m_remainingCharacters > 0
End Get
End Property
Public Function GetNextCharacter() As Char
If m_remainingCharacters < 1 Then
Throw New InvalidOperationException("Cannot retrieve more characters from source")
End If
' We are considering the characters in the range 0 -> # remaining characters
' Get a random element from within that range and store a copy of it to return
' at the end of this iteration
Dim nextCharacterIndex As Integer = m_random.Next(m_remainingCharacters)
Dim returnCharacter As Char = m_allowedCharacters(nextCharacterIndex)
' Place the last character from the range we are considering
' (will not be considered next time) into the place where we
' just got the character from.
m_allowedCharacters(nextCharacterIndex) = m_allowedCharacters(m_remainingCharacters - 1)
' And now decrement the range that we will consider for the
' next iteration
m_remainingCharacters = m_remainingCharacters - 1
Return returnCharacter
End Function
End Class
Usage:
vbnet Code:
Dim random As New System.Random
Dim allowedCharacters As String = "`1234567890-=~!@#$%^&*()_+qwertyuiop[]\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:""zxcvbnm,./ZXCVBNM<>?"
Dim allowedCharacters2 As String = "abc"
For i = 1 To 10
Dim randomCharacters As New RandomCharacters(allowedCharacters2, random)
Do While randomCharacters.CharactersRemaining
Console.Write(randomCharacters.GetNextCharacter)
Loop
Console.WriteLine()
Next
For an explanation of how this algorithm works, see a previous post.
-
Dec 13th, 2010, 05:43 PM
#11
Re: random characters
Originally Posted by dbasnett
Did not know that shag, thanks.
I learned it here. It has something to do with one of the option settings, but I forget which one. Something to make it key compatible with VB6.
My usual boring signature: Nothing
-
Dec 27th, 2010, 08:52 AM
#12
Thread Starter
Lively Member
Re: random characters
I'm sorry but I still don't understand the source.
What I want is that I have a RichTextBox called RichTextBox1, which will get filled with this:
` -> j
1 -> ;
2 -> Z
What I mean by that is that every character will get a new random character which isn't used yet, I hope I explained better this time..
-
Dec 27th, 2010, 09:12 AM
#13
Re: random characters
Code:
Const allowedCH As String = "abcdefghijklmnopqrstuvwxyz1234567890"
Dim prng As New Random
Dim newRandCharString As New System.Text.StringBuilder
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'get a new random character, and remove from list
If newRandCharString.Length = 0 Then
Debug.WriteLine("no more characters")
Exit Sub
End If
Dim idx As Integer = prng.Next(newRandCharString.Length)
RichTextBox1.AppendText(newRandCharString(idx) & Environment.NewLine)
newRandCharString.Remove(idx, 1)
RichTextBox1.ScrollToCaret()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'restart
RichTextBox1.Clear()
newRandCharString.Length = 0
newRandCharString.Append(allowedCH)
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|