weixin_39017744
2019-09-29 02:11
采纳率: 71.1%
浏览 689

请问如何做python的循环随机取数组直到得出指定数字?

举个例子:
随机数字范围:0~100
每组数字量:6(s1,s2,s3,s4,s5,s6)
第二轮开始随机数字范围:新s1和新s2取值为旧s1和s2之间,新s3和新s4取值为旧s3和s4之间,新s5和新s6取值为旧s5和s6之间。
跳出循环条件:任意数字=37
如因s1=s2!=37&s3=s4!=37&s5=s6!=37使数组进入无意义无限循环,则重新取0~100六个数字并开始如上述第二轮随机的随机取值。

如果题目哪里没看懂请告诉我,答案以代码形式发送。谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • 积跬步以至千里。 2019-09-29 11:25
    已采纳
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import random
    
    def random_test():
        # 初始化一个列表,随机0-100的6个数字
        rst_list = [random.randint(0,100) for i in range(0, 6)]
        print(rst_list)
        while 1:
            # 新建临时数据,用来存储rst_list按规则处理后的数据
            temp = []
            for k,v in enumerate(rst_list):
                if k%2==0:
                    temp.append(random.randint(min([rst_list[k],rst_list[k+1]]),max([rst_list[k],rst_list[k+1]])))
                else:
                    temp.append(random.randint(min(rst_list[k-1], rst_list[k]),max(rst_list[k-1], rst_list[k])))
            # 将结果temp列表,写入结果列表 rst_list
            rst_list = temp
            print(rst_list)
            # 判断37在结果列表中,则返回结果列表 rst_list
            if 37 in rst_list:
                print('rst_list:',rst_list)
                return rst_list
            else:
                # 37没在列表中,则按要求判断死循环情况(需求里的 s1=s2!=37&s3=s4!=37&s5=s6!=37 )
                # 如果进入死循环,则重新初始化结果列表 rst_list
                if rst_list[0]==rst_list[1] and rst_list[2]==rst_list[3] and rst_list[4]==rst_list[5]:
                    rst_list = [random.randint(0, 100) for i in range(0, 6)]
    
    def main():
        random_test()
    
    
    if __name__ == '__main__':
        main()
    
    

    根据需求描述,按照最简单和容易理解的方式,逐步实现需求。注释和打印较多,实际代码篇幅并不多

    已采纳该答案
    打赏 评论
  • threenewbee 2019-09-29 09:24

    之前的代码误将调试中的代码贴出,有一行没有写正确,已经修正。

    #!/usr/bin/python
    import random
    
    s=[0,0,0,0,0,0]
    while not(37 in s):
        for i in range(0,6):
            s[i]=random.randint(0,100)
            s.sort()
        while not(37 in s): #这里之前写错了
            s[0]=random.randint(s[0],s[1])
            s[1]=random.randint(s[0],s[1])
            s[2]=random.randint(s[2],s[3])
            s[3]=random.randint(s[2],s[3])
            s[4]=random.randint(s[4],s[5])
            s[5]=random.randint(s[4],s[5])
            if s[0] == s[1] and s[2] == s[3] and s[4] == s[5] and s[0] != 37 and s[2] != 37 and s[4] != 37:
                break
    print(s)
    

    可以再简化下

    #!/usr/bin/python
    import random
    
    s=[0,0,0,0,0,0]
    while not(37 in s):
        for i in range(0,6):
            s[i]=random.randint(0,100)
            s.sort()
        while not(37 in s):
            for j in range(0,6):
                s[j]=random.randint(s[j//2],s[j//2+1])
            if s[0] == s[1] and s[2] == s[3] and s[4] == s[5] and s[0] != 37 and s[2] != 37 and s[4] != 37:
                break
    print(s)
    
    打赏 评论
  • 空白如空 2019-09-29 09:43
    #coding:UTF-8
    import random
    s1 = None
    s2 = None
    s3 = None
    s4 = None
    s5 = None
    s6 = None
    result = None
    
    
    def init_number():
        global s1, s2, s3, s4, s5, s6, result
        result = 37
        s1 = random.randint(init_start, init_end)
        s2 = random.randint(init_start, init_end)
        s3 = random.randint(init_start, init_end)
        s4 = random.randint(init_start, init_end)
        s5 = random.randint(init_start, init_end)
        s6 = random.randint(init_start, init_end)
    
    
    def handler():
        global s1, s2, s3, s4, s5, s6, result
        while s1 != result and s2 != result and s3 != result and s4 != result and s5 != result and s6 != result:
            if s1 < s2:
                new_s1 = random.randint(s1, s2)
                new_s2 = random.randint(s1, s2)
            else:
                new_s1 = random.randint(s2, s1)
                new_s2 = random.randint(s2, s1)
            if s3 < s4:
                new_s3 = random.randint(s3, s4)
                new_s4 = random.randint(s3, s4)
            else:
                new_s3 = random.randint(s4, s3)
                new_s4 = random.randint(s4, s3)
            if s5 < s6:
                new_s5 = random.randint(s5, s6)
                new_s6 = random.randint(s5, s6)
            else:
                new_s5 = random.randint(s6, s5)
                new_s6 = random.randint(s6, s5)
            s1 = new_s1
            s2 = new_s2
            s3 = new_s3
            s4 = new_s4
            s5 = new_s5
            s6 = new_s6
            if s1 == s2 and s1 != result and s3 == s4 and s3 != result and s5 == s6 and s5 != result:
                init_number()
        print s1, s2, s3, s4, s5, s6
    
    
    if __name__ == '__main__':
        init_start = 1
        init_end = 100
        init_number()
        handler()
    
    打赏 评论
  • JonathanYan 2019-09-29 10:18
    import random
    
    s=[0,0,0,0,0,0]
    for i in range(0,6):
        s[i]=random.randint(0,100) 
    
    while not(37 in s):
        for i in range(0,3):
            l, r = min(s[2*i], s[2*i+1]), max(s[2*i], s[2*i+1])
            s[2*i]=random.randint( l, r )
            s[2*i+1]=random.randint( l, r )
    
            while not(37 in s) and s[0] == s[1] and s[2] == s[3] and s[4] == s[5]:
            for i in range(0,6):
                s[i]=random.randint(0,100) 
    
    print(s)
    
    打赏 评论
  • 小小农民 2019-09-30 09:32
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import random
    
    def get_37(list):
    
        if 37 in list:
            print('success_list:', list)
            return list
        if len(list) != 6:
            list = [random.randint(0,100) for i in range(0, 6)]
            print('new list:', list)
        if get_2num_bweet(list,0,1,37) + get_2num_bweet(list,2,3,37) + get_2num_bweet(list,4,5,37) > 0:
            print('check- contuin ',list);
            return get_37(list)
        else:
            print('check- end - list' , list);
            return 0;
    
    def get_2num_bweet(list,i1,i2,b_num):
        if(list[i1] > 37 and list[i2] < 37):
            list[i1:i2+1] = [random.randint(list[i2], list[i1]) for i in range(0, 2)];
            return 1
        elif(list[i2] > 37 and list[i1] < 37):
            list[i1:i2+1] = [random.randint(list[i1], list[i2]) for i in range(0, 2)];
            return 1
        else:
            list[i1] = list[i2]
            return 0
    
    def main():
        list = [random.randint(0, 100) for i in range(0, 6)]
        while get_37(list) == 0:
            list = [random.randint(0, 100) for i in range(0, 6)]
            print('new list:', list)
    
    
    if __name__ == '__main__':
        main()
    
    
    打赏 评论

相关推荐 更多相似问题