Welcome To Darwinbots > Newbie
Methinks it is like a weasel
asterixx:
Well, I succeeded at making a completely different program. Its a random number generator which I think has helped me get my brain around this. Is this at least a good first step in figuring it out?
--- Quote ---Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Numb As Integer = Rand(1, 1000)
Label1.Text = (Numb.ToString())
End Sub
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd()) + Low
End Function
End Class
--- End quote ---
That can pretty much be copy/pasted into VB, just make a Button1 and a Label1.
Numsgil:
Yep, that's a good first step. The trick now is to realize that a string is stored in memory using chars, and that when you're using chars, something like 'a' is really just a number. So for the next step, try to build something that generates random letters that you're interested in (eg: A-Z, and maybe some punctuation marks). Bonus points if you can do it with less than half a dozen if/else cases.
goffrie:
--- Code: ---#include <stdio.h>
#include <time.h>
#include <string.h>
char str[1000];
const char ref[1000] = "Methinks it is like a weasel";
int main() {
int I, len;
srand(time(NULL));
len = strlen(ref);
while (strcmp(str, ref) != 0) { /* if we're not done */
for (I = 0; I < len; ++I) { /* iterate through each character */
if (str[I] != ref[I]) /* only if it isn't correct already */
str[I] = rand() % 95 + 32; /* set to random character */
/* 32 = space, 95 printable characters */
/* !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQR
STUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ */
}
printf("%s\r", str); /* spit out the current string */
usleep(5000); /* sleep - without it, the program ends almost immediately */
}
printf("\n");
return 0;
}
--- End code ---
Yay C but probably coded just as easily in any other language.
asterixx:
[div class=\'codetop\']CODE[div class=\'codemain\' style=\'height:200px;white-space:pre;overflow:auto\']Module Module1
Sub Main()
Dim Create As StringCreator
Dim NumberOfKeys As Integer
Dim i_Keys As Integer
Dim RandomCreate As String
NumberOfKeys = 1
Create = New StringCreator
Create.KeyLetters = "abcdefghijklmnopqrstuvwxyz"
Create.KeyNumbers = "0123456789"
Create.KeyChars = 10
For i_Keys = 1 To NumberOfKeys
RandomCreate = Create.Generate()
Console.WriteLine(RandomCreate)
Next
Console.WriteLine("Press any key to exit...")
Console.Read()
End Sub
End Module
////////////////////////////
Option Strict On
Imports System.Text
Public Class StringCreator
Dim Key_Letters As String
Dim Key_Numbers As String
Dim Key_Chars As Integer
Dim LettersArray As Char()
Dim NumbersArray As Char()
Property KeyLetters() As String
Set(ByVal Value As String)
Key_Letters = Value
End Set
End Property
Property KeyNumbers() As String
Set(ByVal Value As String)
Key_Letters = Value
End Set
End Property
Property Set KeyChars() As String
Set(ByVal Value As String)
Key_Letters = Value
End Set
End Property
Function Generate() As String
Dim i_key As Integer
Dim Random1 As Single
Dim arrIndex As Int16
Dim sb As New StringBuilder
Dim RandomLetter As String
LettersArray = Key_Letters.ToCharArray
NumbersArray = Key_Numbers.ToCharArray
For i_key = 1 To Key_Chars
Randomize()
Random1 = Rnd()
arrIndex = -1
If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then
Do While arrIndex < 0
arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * Random1)
Loop
RandomLetter = LettersArray(arrIndex)
If (CType(arrIndex * Random1 * 99, Integer)) Mod 2 <> 0 Then
RandomLetter = LettersArray(arrIndex).ToString
RandomLetter = RandomLetter.ToUpper
End If
sb.Append(RandomLetter)
Else
Do While arrIndex < 0
arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1)
Loop
sb.Append(NumbersArray(arrIndex))
End If
Next
Return sb.ToString
End Function
End Class
Ok, this one generates a random 10 character string. Its in VB as well.
Navigation
[0] Message Index
[*] Previous page
Go to full version