Foxtable(狐表)用户栏目专家坐堂 → [求助] 随机组合出现重复怎么办?


  共有5089人关注过本帖树形打印复制链接

主题:[求助] 随机组合出现重复怎么办?

帅哥哟,离线,有人找我吗?
农村人
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:174 积分:2349 威望:0 精华:0 注册:2015/8/12 15:36:00
[求助] 随机组合出现重复怎么办?  发帖心情 Post By:2016/2/29 13:05:00 [显示全部帖子]

使用如下代码;
Dim str() As String = {"1","2","3","4","5","6"}
For i As Integer = 1 To 200 '组合次数
    Dim rnum1 As Integer = Rand.Next(str.Length)
    Dim rnum2 As Integer = Rand.Next(str.Length)
    Dim temp As String = str(rnum1)
    str(rnum1) = str(rnum2)
    str(rnum2) = temp
    Dim data As String = String.Join("", str)
    Output.Show(data)
Next

组合结果会出现多个重复,请问这如何解决?

 回到顶部
帅哥哟,离线,有人找我吗?
农村人
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:174 积分:2349 威望:0 精华:0 注册:2015/8/12 15:36:00
  发帖心情 Post By:2016/3/1 14:49:00 [显示全部帖子]

你好,这样仍然会出现重复的组合的

 回到顶部
帅哥哟,离线,有人找我吗?
农村人
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:174 积分:2349 威望:0 精华:0 注册:2015/8/12 15:36:00
  发帖心情 Post By:2016/3/1 15:21:00 [显示全部帖子]

Dim str() As String = {"1","2","3","4","5","6"}
Dim ls As new List(Of String)
For i As Integer = 1 To 200
    For j As Integer = 1 To 5 '洗牌次数   ----这个代码主要起到什么作用的呢?
        Dim rnum1 As Integer = Rand.Next(str.Length)
        Dim rnum2 As Integer = Rand.Next(str.Length)
        Dim temp As String = str(rnum1)
        str(rnum1) = str(rnum2)
        str(rnum2) = temp
    Next
    Dim data As String = String.Join("", str)
    If ls.Contains(data) = False Then
        Output.Show(i & " " & data)
    Else
        i -=1
    End If
Next

 回到顶部
帅哥哟,离线,有人找我吗?
农村人
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:174 积分:2349 威望:0 精华:0 注册:2015/8/12 15:36:00
  发帖心情 Post By:2016/3/1 15:26:00 [显示全部帖子]

非常感谢您的耐心解答,

Dim str() As String = {"1","2","3","4","5","6"}
Dim ls As new List(Of String)
For i As Integer = 1 To 200
    For j As Integer = 1 To 5 '洗牌次数   ----这个代码主要起到什么作用的呢?
        Dim rnum1 As Integer = Rand.Next(str.Length)
        Dim rnum2 As Integer = Rand.Next(str.Length)
        Dim temp As String = str(rnum1)
        str(rnum1) = str(rnum2)
        str(rnum2) = temp
    Next
    Dim data As String = String.Join("", str)
    If ls.Contains(data) = False Then
        Output.Show(i & " " & data)
    Else
        i -=1
    End If
Next

仍然会出现重复

 回到顶部
帅哥哟,离线,有人找我吗?
农村人
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:174 积分:2349 威望:0 精华:0 注册:2015/8/12 15:36:00
  发帖心情 Post By:2016/3/1 16:37:00 [显示全部帖子]

OK了,谢谢 大红袍

我现在需要再设置一个字符串,例如:"{"上","中","下","东","南","西","北"} 按照组合数量,有顺序地加入到循环组合,
Dim str() As String = {"1","2","3","4","5","6"}
Dim ls As new List(Of String)
For i As Integer = 1 To 100  '组合数量
    For j As Integer = 1 To 5 '洗牌次数  
        Dim rnum1 As Integer = Rand.Next(str.Length)
        Dim rnum2 As Integer = Rand.Next(str.Length)
        Dim temp As String = str(rnum1)
        str(rnum1) = str(rnum2)
        str(rnum2) = temp
    Next
    Dim data As String = String.Join("", str)
    If ls.Contains(data) = False Then
        ls.add(data)
        Output.Show(i & " " & data)
    Else
        i -=1
    End If
Next

 回到顶部