6 Replies - 3209 Views - Last Post: 30 January 2013 - 06:53 AM Rate Topic: -----

#1 Ganelon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 13

VB 2010 Express - Choosing an array value NOT chosen previously

Posted 29 January 2013 - 04:53 PM

I'm using Visual Basic 2010 Express.

Okay, here's my issue. I'm making a program that encrypts text so that my friends and I can mess around with secret messages in school (yeah, it's sad, but I think it'll be good practice for coding :P/>). I have an array of 251 strings. I want my program to choose a random one of these strings, but NOT one of them that was already selected previously. I've been trying to use a random number generator for the last few hours, but that won't work with the limited knowledge I have now.

This is my code:
 generator_counter = 1

        Do While generator_counter <= Len(input_text)

            'this is my random number generator; it randomly chooses one of 251 values.
            Randomize()
            rdm_select = Int(Rnd() * 250)


            'my thinking was this: If I can make it choose a random number BUT NOT A NUMBER IT CHOSE ALREADY, this would work:

                output_code = output_code + (Mid(input_text, generator_counter, 1) = code_chars(rdm_select))

            'I figure there has to be a way to, without the random number generator, make it choose a random string from the
            'array that was not already used; giving me (in words, not code):
            'output_code = output_code + [a value from the array not used previously]

            generator_counter = generator_counter + 1
        Loop


Can anyone, please, explain to me how to do one of these two things:
  • Make a random number generator NOT choose a previously chosen number (without a DO WHILE loop)
  • Make the program select one of the strings in the array randomly, but NOT one chosen previously


I deleted the code I had before, because it wasn't gonna work. It involved a convoluted process that, even if I had made it work, would just result in it going around the DO WHILE over and over until the random number generator generated a number that hadn't been used before. Ignore the fact that all of this is inside a DO WHILE; that's crucial to the program and shouldn't affect what I need to do.

tl;dr: The most helpful thing would be for someone to teach me how to make a random number generator not choose a number chosen previously.

Is This A Good Question/Topic? 0
  • +

Replies To: VB 2010 Express - Choosing an array value NOT chosen previously

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4444
  • View blog
  • Posts: 12,318
  • Joined: 18-April 07

Re: VB 2010 Express - Choosing an array value NOT chosen previously

Posted 29 January 2013 - 05:32 PM

If you create an arraylist that contains all the numbers in the range (so if you want 1 through 50 you would create an arraylist of 1 through 50 numbers) then you have two options:

1) You can randomly select an item using its index and doing a ArrayList.Remove() method call on it to remove it from the list entirely.

2) Shuffle the array values around randomly (one way to do this is to do random swaps... pick two indexes in the array randomly and then swap the values) and then just start popping off elements from the beginning using the ArrayList.Remove() method.

So the idea behind these two methods is that you have a set number of elements in the list and by removing a random one each time it leaves the list 1 shorter until you eventually run out of items in the list to choose.

:)
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,596
  • Joined: 12-December 12

Re: VB 2010 Express - Choosing an array value NOT chosen previously

Posted 29 January 2013 - 05:34 PM

Rnd() obtains a random number which includes 0 but doesn't include 1. So

rdm_select = Int(Rnd() * 250)

generates numbers between 0 and 249. Use
rdm_select = Int(Rnd() * 251)

which generates a number between 0 and 250 (251 items), bearing in mind that your array of strings starts at 0. To elaborate:

Dim yourArray(251) As String

generates an array of elements yourArray(0) to yourArray(251) (252 items).

Anyway, you could store each random number generated in a List and then, each time you attempt to create a new random number, check whether it exists in the list (using List.Contains()). You would still need a loop though, to generate the numbers.

Added: Martyr2's first method avoids the need to repeatly loop when generating the random numbers.

This post has been edited by andrewsw: 29 January 2013 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,596
  • Joined: 12-December 12

Re: VB 2010 Express - Choosing an array value NOT chosen previously

Posted 29 January 2013 - 05:40 PM

BTW You should prefer the newer Random Class to the use of Rnd(). It doesn't matter too much but, if you are learning, you might as well get with the modern program :)/>

This post has been edited by andrewsw: 29 January 2013 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,596
  • Joined: 12-December 12

Re: VB 2010 Express - Choosing an array value NOT chosen previously

Posted 29 January 2013 - 07:04 PM

The following may be of interest:

        'fill a list with sequential numbers:
        'Dim aRange As List(Of Integer) = Enumerable.Range(1, 10).ToList()
        Dim rand As New Random()

        'fill a list with 50 random numbers between 0 and 250:
        Dim aRange As List(Of Integer) = Enumerable.Range(1, 50).Select(Function(x) rand.Next(0, 250)).ToList()
        Dim y As Integer = 1

        For Each item In aRange
            Console.WriteLine("Index: {0}, {1}", y, item)
            y += 1
        Next

Was This Post Helpful? 0
  • +
  • -

#6 Ganelon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 13

Re: VB 2010 Express - Choosing an array value NOT chosen previously

Posted 30 January 2013 - 01:57 AM

View PostMartyr2, on 29 January 2013 - 05:32 PM, said:

1) You can randomly select an item using its index and doing a ArrayList.Remove() method call on it to remove it from the list entirely.


I LOVE YOU. Thank you SO much; I'll try this as soon as I get on VB. Only issue is, how would I add them back to the array at the end? After the DO WHILE just put something like ArrayList.Add()? How would I ensure that all the strings in the array were restored, keeping in mind that not all of them were necessarily removed?
Was This Post Helpful? 0
  • +
  • -

#7 ybadragon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,534
  • Joined: 11-May 12

Re: VB 2010 Express - Choosing an array value NOT chosen previously

Posted 30 January 2013 - 06:53 AM

Quote

Only issue is, how would I add them back to the array at the end? After the DO WHILE just put something like ArrayList.Add()? How would I ensure that all the strings in the array were restored, keeping in mind that not all of them were necessarily removed?


Create a second list, and before you remove your item from the original list, add it to the second. Then after the Do While loop, create a second loop such as

Dim secondList As New ArrayList

For each s as String in secondList
firstList.Add(s)
Next


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1