-
Dec 22nd, 2011, 10:50 AM
#1
Thread Starter
New Member
Array of string and Random
Hello everyone
I have a question about an array of string and random.
I have an arry of string i.e
Dim testArrayString() As String = {"Orange", "Apple", "London", "Sydney", "anything"}
I want to show the each element of the above array randomly one by one in a message box....and also stores that randomly generated string from that array into an another variable... How would i do that... i have no clue
please help....
Thanks
Usman
-
Dec 22nd, 2011, 10:54 AM
#2
Re: Array of string and Random
That doesn't actually sound random. It sounds like you want to visit each one only once. Is that the case, or do you truly want it to be random (in which case you would have multiples of some, skip others, and so forth)?
My usual boring signature: Nothing
-
Dec 22nd, 2011, 11:48 AM
#3
Thread Starter
New Member
Re: Array of string and Random
I have work it out thanks you very much guys
the following code is i got it and it is working
Code:
Dim testArrayString() As String = {"Orange", "Apple", "London", "Sydney", "anything"}
Dim validString As String
Private Sub generateString()
Dim randomIndex As Integer
Dim length As Integer
Dim randomClass As New Random
length = testArrayString.Length - 1
randomIndex = randomClass.Next(0, length)
validString = testArrayString(randomIndex)
ListBox1.Items.Insert(0, validString)
End Sub
if you people can advice me to make this code more better please go ahead
thanks
USMAN
Last edited by sh4rif; Dec 22nd, 2011 at 11:53 AM.
-
Dec 22nd, 2011, 12:11 PM
#4
Re: Array of string and Random
It would be somewhat safer to move the declaration of the Random object outside of the method. This may not apply in your case, but you should be aware of the danger with that object. When you create a new Random object, the random number generator gets seeded with a value. The sequence produced is not really random, but appears to be random. If you were to seed it again with the same value, then you would get the exact same sequence. Each seed produces a different sequence, but identical seeds produce identical sequences. This is useful for testing, because you can supply a seed and know what sequence of 'random' numbers you will get. Normally, you don't supply a seed, in which case the system time is used down to the second. This is where the danger lies. If you were to call that method in some kind of loop, such that new random objects are created several times a second, then all of those sequences will be exactly the same until the system time advances by one second.
Having a single Random object at class scope is safer, as you will never fall into the problem of creating random objects in a tight loop that way. It would be safer still for the Random object to be declared as Shared, but that may be a step more than is really necessary.
My usual boring signature: Nothing
-
Dec 22nd, 2011, 12:17 PM
#5
Frenzied Member
Re: Array of string and Random
please explain what you are trying to do in small steps please
Does you code actually work?
I think you want to show the words randomly and you want them to be shown once and once only
here is away to do it that ensures the onceness if there is such a word
build your array
find the size ubound(array)
now select 2 random numbers from within the arran index realm i.e. 0 to ubound(array)-1
using a temp_string
put array(r1) into the temp_str
put the array(r2) into array(r1)
put temp_str into array(r2)
do that many times and you will end up with a mixed up version of your first array
simply output the array to a storage structure array or string whatever
now present your array to the viewer in order
for pos=0 to ubound(array)
fnshow array(pos)
next pos
where fnshow is what ever activity you wish to use to present the array contents
is that more like what you meant
oh ans seed the random generators with a unknown variable val;ue like the time or the (time * month) or the number of seconds since your birthday, just so long as the value is unknown and likely to be different each time tyou call the routine!
here to talk
-
Dec 22nd, 2011, 12:23 PM
#6
Re: Array of string and Random
I do not know if this is more better but its shorter anyway:
vb.net Code:
Dim testArrayString() As String = {"Orange", "Apple", "London", "Sydney", "anything"}
Dim randomClass As New Random
Private Sub generateString()
ListBox1.Items.Add(testArrayString(randomClass.Next(testArrayString.Length)))
End Sub
I used your exact same logic just in one line without so many variables.
Three things I changed:
1) I got rid of the "-1" since the upper bound is "Exclusive" that means that it will never be generated, and using it you will never get the last element in your array.
2) You do not need a lower bound index if it is Zero.
3) Using Add instead of Insert you do not need an index for that either. The difference is it will add the item at the bottom instead of at the top.
Last edited by kaliman79912; Dec 22nd, 2011 at 12:52 PM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
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
|