Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I would like to generate Random string of fixed length.The code is below.

VB
Private Shared Function RandomString(Size As Integer) As String
        Dim input As String = "abcdefghijklmnopqrstuvwxyz0123456789"
        Dim chars = Enumerable.Range(0, Size).[Select](Function(x) input(Random.[Next](0, input.Length)))
        Return New String(chars.ToArray())
    End Function


But I receive an error of

Quote:
Overload resolution failed because no accessible 'Select' can be called with these arguments:
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Integer, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of Integer, Integer, TResult)'.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Integer, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.


Regards
Sreejith
Posted
Updated 13-May-15 9:48am
v2

Try this:
VB
Private Shared chars As String = "abcdefghijklmnopqrstuvwxyz0123456789"
Private random As New Random()
Private Function RandomString(size As Integer) As String
    Return New String(Enumerable.Repeat(chars, size).[Select](Function(s) s(random.[Next](s.Length))).ToArray())
End Function
 
Share this answer
 
Comments
Maciej Los 13-May-15 16:15pm    
Hmmm... Have you tested it, Paul?
OriginalGriff 13-May-15 16:33pm    
Wrote and tested in C#, then translated via Telerik:
http://converter.telerik.com/
It normally gets it right...
Though quite what the HTML is doing in there I don't know...
OriginalGriff 13-May-15 16:35pm    
Interesting - it's a CP bug.
I suspect Markdown... I'll get it reported to the hamsters.
Maciej Los 13-May-15 16:48pm    
5ed!
Have a look at my answer ;)
OriginalGriff 13-May-15 17:43pm    
Are you channelling He Who Must Not Be Named? :laugh:
Random needs to be intialized! Please see: Ranodm Class (System)[^]

This works as well:
VB
Private Function RandomString(size As Integer) As String

	Dim input As String = "abcdefghijklmnopqrstuvwxyz0123456789"
	Dim r As New Random
	Dim qry = Enumerable.Range(0, Size).[Select](Function(x) input(r.Next(0, input.Length)))
	Return New String(qry.ToArray())
   
End Function


Usage and result:
VB
Dim a As String = RandomString(55)
'result: y0lchxdvh8yaz73tbu1q0h6uptpkm19ldso8ozm64pnk7bvum86uz9e
 
Share this answer
 
OK, there seems to be a site bug which is corrupting the VB code.
Let's see if we can get it readable:
VB
Private Shared chars As String = "abcdefghijklmnopqrstuvwxyz0123456789"
Private random As New Random()
Private Function RandomString(size As Integer) As String
    Return New String(Enumerable.Repeat(chars, size). [Select]



(Function(s) s( random. [Next]




(s.Length))) .ToArray())
End Function


Assemble the bits into a single line!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900