vb.net 随机出数字,但要求包含特定数字如何做?
用户账户,要求随机10位并且里面包含4个6或4个8如何实现这样的呢?1238587880或1236567660这种类型...
用户账户,要求随机10位 并且里面包含4个6或4个8
如何实现这样的呢?
1238587880或1236567660这种类型 展开
如何实现这样的呢?
1238587880或1236567660这种类型 展开
2个回答
展开全部
下面是你需要的,经过三次随机,可以看一下,直接调用函数返回一个字符就是你要的,看不懂再追问哈
Structure jiqi
Dim num As Integer
Dim num2 As Integer
End Structure
Public Function chansheng() As String
Dim str As String = ""
Dim n As Integer = 0
Dim p68 As Integer
Randomize()
p68 = Int(Rnd() * 2) + 1
Dim a(10), b(8) As Integer
For i = 1 To 6
b(i) = i - 1
Next
b(7) = 7
b(8) = 9
For i = 1 To 6
Randomize()
a(i) = b(Int(Rnd() * 8) + 1)
Next
If p68 = 1 Then
For i = 7 To 10
a(i) = 6
Next
ElseIf p68 = 2 Then
For i = 7 To 10
a(i) = 8
Next
End If
Dim c(10) As Integer
Dim temp As jiqi
Dim pi(10) As jiqi
For i = 1 To 10
pi(i).num = a(i)
pi(i).num2 = Int(Rnd() * 1000) + 10
Next
For i = 1 To 10
For j = 1 To 10 - i
If pi(j).num2 > pi(j + 1).num2 Then
temp = pi(j + 1)
pi(j + 1) = pi(j)
pi(j) = temp
End If
Next j
Next i
For i = 1 To 10
str = str & pi(i).num
Next
Return str
End Function
2015-05-28 · 知道合伙人互联网行家
关注
展开全部
自己设置一个方法(或者包装系统的Random类,这个更难,不过更专业),下面给你代码:
public int getRandomExcept(int RandMax,int[] ExceptNums){
Random rand=new Random();
int num=rand.nextInt(RandMax);
while(true){
int have=0;
for(int i=0;i<ExceptNums.length;i++){
if(num==ExceptNums[i]){
have=1;
}
}
if(have==0){
return num;
}
num=rand.nextInt(RandMax);
}
}
public int getRandomExcept(int RandMax,int[] ExceptNums){
Random rand=new Random();
int num=rand.nextInt(RandMax);
while(true){
int have=0;
for(int i=0;i<ExceptNums.length;i++){
if(num==ExceptNums[i]){
have=1;
}
}
if(have==0){
return num;
}
num=rand.nextInt(RandMax);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询