0

Hello i would like to get 2 different random characters to string. I can get 1 array only the second random characters seem to be get from the first random characters.

What am i doing wrong? Here's the code :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For i = 1 To 10

    Dim chars = "abcdefghijklmnopqrstuvwxyzZ0123456789"
    Dim random = New Random()
    Dim result = New String(Enumerable.Repeat(chars, 3).[Select](Function(s) s(random.[Next](s.Length))).ToArray())

    firstpart = TextBox2.Text
    rm = firstpart & result



    Dim chars = "0123456789"
    Dim random = New Random()
    Dim result = New String(Enumerable.Repeat(chars, 3).[Select](Function(s) s(random.[Next](s.Length))).ToArray())

    nextpart = TextBox3.Text
    rn = nextpart & result

now i would like to call a second random character but only numbers, can anyone help or explain this to me.

6
  • It's not clear what the problem is. What is the output of your code and how is it different from what you expect? Commented Jan 2, 2013 at 16:00
  • this is what i would like to get for example rm=ab1 and rn=123 Commented Jan 2, 2013 at 16:02
  • @David above is what i would like to get as outcome for example. i want 2 different outcome for it Commented Jan 2, 2013 at 16:13
  • And how is the code not producing that output? What is the code producing? Commented Jan 2, 2013 at 16:14
  • it is producing the same output as the ( firstpart ) so it is taking that random chars when call "rn" Commented Jan 2, 2013 at 16:23

2 Answers 2

2

You are creating a new Random object each time you iterate through your loop. You need to create it once, before entering the loop. Keep in mind that computer generated random numbers are not truly random, they only appear to be random. Each time you generate a random number, it is just returning the next number in the sequence of "random" numbers that based on an original time-based seed value. When you create a new Random object, it seeds the randomizing algorithm with the current time. Therefore, if you create two Random objects at the same time, they will both generate the same sequence of numbers. Since your loop is very fast, all of the Random objects that you are instantiating all just happen to be getting seeded with the same time.

Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks for the tip, but now my question is how can i call a different random char in this script??
1

Although your coding needs to be changed as Steven mentioned in his answer, it actually seems to work as you mention wanting it to - I tested it and got the first string to be a mixture of characters and numbers and the second to only be numbers. Here's what I wrote based upon your code:

    Dim rm As String = ""
    Dim rn As String = ""
    Dim chars As String = "abcdefghijklmnopqrstuvwxyzZ0123456789"
    Dim nums As String = "0123456789"
    Dim random = New Random()
    Dim result as string = ""

    For i = 1 To 10

        result = New String(Enumerable.Repeat(chars, 3).[Select](Function(s) s(random.[Next](s.Length))).ToArray())

        rm = rm & result


        result = New String(Enumerable.Repeat(nums, 3).[Select](Function(s) s(random.[Next](s.Length))).ToArray())

        rn = rn & result

    Next

3 Comments

Ok this helped really thanks for the great information, now one last thing how can i call the loop 10 times using that as a textbox so that i can put 10 in a textbox as input for the looptime, this works for c# for (var i = 0; i < Int32.Parse(textBox2.Text); i++) { int yourRandomStringLength = 12; //maximum: 32 Guid.NewGuid().ToString("N").Substring(0, yourRandomStringLength); {
thmx :) i got it working. Great site SO the answers and help over here is absolute GREAT!
Glad you got it working and welcome to the community - I know it's helped me IMMENSELY... Keep posting and try and help out as many others as you can along the way :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.