随机生成字符串 [英] Random Generation string
问题描述
大家好,
我想生成固定长度的随机字符串。代码如下。
Hi All,
I would like to generate Random string of fixed length.The code is below.
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
重载决策失败,因为没有可访问的'Select'可以使用这些参数调用:
扩展方法'公共函数选择(Of TResult)(selector As System.Func(Of Integer) ,Integer,TResult))作为System.Collections.Generic.IEnumerable(Of TResult)'在'System.Linq.Enumerable'中定义:嵌套函数没有与委托'System.Func兼容的签名(Of Integer,Integer) ,TResult)'。
扩展方法'Public Function Select(Of TResult)(selector As System.Func(Of Integer,Integer,TResult))As System.Collections.Generic.IEnumerable(Of TResult) '定义在'System.Linq.Enumerable'中:无法从这些参数推断出类型参数的数据类型。明确指定数据类型可能会纠正此错误。
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.
问候
Sreejith
Regards
Sreejith
推荐答案
试试这个:
Try this:
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
随机
需要初始化!请参阅: Ranodm Class(System) [< a href =https://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]
这也有效:
Random
needs to be intialized! Please see: Ranodm Class (System)[^]
This works as well:
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:
Dim a As String = RandomString(55)
'result: y0lchxdvh8yaz73tbu1q0h6uptpkm19ldso8ozm64pnk7bvum86uz9e
好的,似乎有一个网站错误破坏了VB代码。
让我们看看我们是否可以得到它可读:
OK, there seems to be a site bug which is corrupting the VB code.
Let's see if we can get it readable:
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!
这篇关于随机生成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!