9 Replies - 5141 Views - Last Post: 12 July 2010 - 12:15 PM Rate Topic: -----

#1 SexySquirrel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 30-August 09

Generate random string

Posted 11 July 2010 - 11:57 PM

Hey guys, its been a while since i last posted anything.. :P

So what I'm trying to do is not so much generate a random word but pick one from a collection.

I was thinking on generating a random number since that is very easy, then picking a word acording to the number generated.


Is there a more efficient way of doing this?

Is This A Good Question/Topic? 0
  • +

Replies To: Generate random string

#2 Guest_CPee*


Reputation:

Re: Generate random string

Posted 12 July 2010 - 01:30 AM

dim w as string

w = int(rnd() * 10)

select case w

case 1
textbox1.text = "word here"
case 2
textbox1.text = "word here"
case 3
textbox1.text = "word here"

end select



Was This Post Helpful? 0

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Generate random string

Posted 12 July 2010 - 02:32 AM

that is a possible way but it will be easy with collection or an array. how ever be careful the random is not very random.
Dim ran as New Random(System.DateTime.Now.Millisecond)



random is connected to the time so in order to get random numbers you have to set a time to mix the numbers. in the example 1 millisecond.

EDIT: one more thing the code that our guest show you is a little native(well a lot) so i dont suggest you to use it.

i hope this make some sense :P

This post has been edited by NoBrain: 12 July 2010 - 02:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Generate random string

Posted 12 July 2010 - 06:44 AM

View PostSexySquirrel, on 11 July 2010 - 11:57 PM, said:

Hey guys, its been a while since i last posted anything.. :P

So what I'm trying to do is not so much generate a random word but pick one from a collection.

I was thinking on generating a random number since that is very easy, then picking a word acording to the number generated.


Is there a more efficient way of doing this?

Try this:
Public Function GenerateString(Byval numberofchars As Integer) As String
Dim letters As New String() {"abcdefghijklmnopqrstuvwxyz"}
Dim str As String = ""
For I As Integer = 0 To numberofchars
str = str + letters(new System.Random().Next(0,letters.Length + 1))
Next
Return str
End Function


And now you can call it using
Dim randomstring = GenerateString(7) 'Generates a 7 char string.


I hope this helps and good luck! :donatello:
EDIT: Oh, i see you just wanted a word. Thats easy with a simple code change.
Public Function GenerateWord(Byval words As String()) As String
Return words(new System.Random().Next(0,words.Length + 1))
End Function


And now, you can call it using.
Di words As new String() {"word1","word2","word3"}
Dim randomword = GenerateWord(words)


Now, good luck! :smartass:

This post has been edited by Amrykid: 12 July 2010 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4430
  • View blog
  • Posts: 7,700
  • Joined: 08-June 10

Re: Generate random string

Posted 12 July 2010 - 07:13 AM

View PostSexySquirrel, on 12 July 2010 - 05:57 AM, said:

So what I'm trying to do is not so much generate a random word but pick one from a collection.

I was thinking on generating a random number since that is very easy, then picking a word acording to the number generated.



You've got the right of it. How do you plan on initially loading your words? Hard coded, or from a file? Either way, load them into a List(of String), generate a random int between 0 and your list's length, and use the word at that index.

@NoBrain
Random is by default seeded by the system clock's ticks, which is even more granular than the milliseconds.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4430
  • View blog
  • Posts: 7,700
  • Joined: 08-June 10

Re: Generate random string

Posted 12 July 2010 - 07:13 AM

I have no idea how this double posted.

This post has been edited by insertAlias: 12 July 2010 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Generate random string

Posted 12 July 2010 - 07:45 AM

View PostinsertAlias, on 12 July 2010 - 03:13 PM, said:

@NoBrain
Random is by default seeded by the system clock's ticks, which is even more granular than the milliseconds.


first the clock ticks are in Milliseconds. second that was example and i suggest you use more then millisecond for it. in debug mode the things look fine and dandy but if you release it the only random you get is +- 1 from the first number generated (usually 1).
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4430
  • View blog
  • Posts: 7,700
  • Joined: 08-June 10

Re: Generate random string

Posted 12 July 2010 - 07:53 AM

Hmm. If "ticks" means the same thing in both contexts...

Quote

By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time.

http://msdn.microsof...tem.random.aspx

Quote

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

http://msdn.microsof...time.ticks.aspx

That would suggest that ticks are much more granular than milliseconds.

Although upon rereading the first quote, I did misread it. It says that the parameterless constructor uses the system clock, but it doesn't say which part. Also, DateTime.Ticks is an Int64, and Random's constructor expects an Int32. So I very well could be wrong.
Was This Post Helpful? 0
  • +
  • -

#9 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Generate random string

Posted 12 July 2010 - 08:03 AM

as far as i know but it is limited from GetTickCount() API function witch returns the uptime of the system. so it return in int32 the milliseconds from the start of the system(witch eventually resets). I do think the use the same function for the random in vb.net. but i maybe wrong too :)

This post has been edited by NoBrain: 12 July 2010 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Generate random string

Posted 12 July 2010 - 12:15 PM

When combined the following two extension method can generate a random string.
UpTo
GetRnd


Eg
Dim s As String="A"c.UpTo("Z").GetRnd(10)


s will contain 10 random chars,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1