How to Generate All 5-Number Combinations Containing Adjacent Values Using Python
This article walks through solving a combinatorial challenge—selecting 5 numbers from a random set of 15 such that a chosen number and its consecutive successor both appear—by presenting multiple Python implementations, from basic random sampling to optimized Monte‑Carlo approaches, complete with code snippets and performance insights.
Introduction
The author, a Python enthusiast, shares a solution to a fan’s combinatorial question about selecting numbers that contain a pair of consecutive values.
Problem Statement
Given a list of 15 distinct random numbers, choose any 5 numbers such that the selection includes a number a and its immediate successor a+1, where a can be any element of the original list.
Idea
The problem can be tackled in two steps: first generate a set of random numbers, then check whether a randomly chosen pair of consecutive numbers appears together in a 5‑element subset. The author references a Monte Carlo approach to enumerate valid combinations.
Solution
1) Pseudocode
The following pseudocode outlines the overall workflow: generate 14 unique random numbers, add the successor of a randomly chosen number to form 15 numbers, then repeatedly sample 5 numbers and keep those containing both the target pair.
2) Code 1
# coding: utf-8
import random
def quchong(list_data):
list2 = []
for i in list_data:
if i not in list2:
list2.append(i)
return list2
# generate 14 unique random numbers
random_14 = random.sample(range(100), 14)
print(random_14)
random_1 = random.choice(random_14)
random_2 = random_1 + 1
random_14.append(random_2)
random_15 = random_14
print(random_1, random_2)
final_list = []
for i in range(100):
select = [random.choice(random_15) for j in range(5)]
quchong_select = set(select)
if random_1 in quchong_select and random_2 in quchong_select:
final_list.append(quchong_select)
fina_result = quchong(final_list)
print(len(fina_result))3) Code 2
# -*- coding: utf-8 -*-
import numpy as np
import random
def quchong(list_data):
list2 = []
for i in list_data:
if i not in list2:
list2.append(i)
return list2
random_14 = random.sample(range(100), 14)
print(random_14)
random_1 = random.choice(random_14)
random_2 = random_1 + 1
random_14.append(random_2)
random_15 = random_14
print(random_1, random_2)
final_list = []
for i in range(100):
sub_random_data = np.random.choice(random_15, 5)
quchong_select = set(sub_random_data)
if random_1 in quchong_select and random_2 in quchong_select:
final_list.append(quchong_select)
fina_result = quchong(final_list)
print(len(fina_result))4) Code 3
# -*- coding: utf-8 -*-
import random
import numpy as np
def get_random15():
random_14 = random.sample(range(1000), 14)
print(random_14)
random_1 = random.choice(random_14)
random_2 = random_1 + 1
random_14.append(random_2)
random_15 = random_14
print(random_1, random_2)
get_final_result(random_1, random_2, random_15)
def get_final_result(random_1, random_2, random_15):
final_list = []
for i in range(1000):
sub_random_data = np.random.choice(random_15, 5)
quchong_select = set(sub_random_data)
if random_1 in quchong_select and random_2 in quchong_select:
final_list.append(quchong_select)
fina_result = quchong(final_list)
print(len(fina_result))
def quchong(list_data):
list2 = []
for i in list_data:
if i not in list2:
list2.append(i)
return list2
if __name__ == '__main__':
get_random15()5) Code 4
import random
import numpy as np
def get_random15():
for i in range(2):
random_15 = random.sample(range(20), 15)
get_random5(random_15)
def get_random5(random_15):
random_5 = []
for i in range(5):
random_data = np.random.choice(random_15)
random_5.append(random_data)
random_15.remove(random_data)
for num in random_5:
random_1 = num
random_2 = random_1 + 1
get_final_result(random_1, random_2, random_5)
def get_final_result(random_1, random_2, random_5):
final_list = []
if random_1 in random_5 and random_2 in random_5:
print(random_5)
final_list.append(random_1)
result = quchong(final_list)
print(result)
def quchong(list_data):
list = []
for i in list_data:
if i not in list:
list.append(i)
return list
if __name__ == '__main__':
get_random15()6) Code 5
# -*- coding: utf-8 -*-
import random
import numpy as np
import time
def get_random15():
for i in range(100000):
random_15 = random.sample(range(2000), 15)
get_random5(random_15)
def get_random5(random_15):
random_5 = []
for i in range(5):
random_data = np.random.choice(random_15)
random_5.append(random_data)
random_15.remove(random_data)
for num in random_5:
random_1 = num
random_2 = random_1 + 1
get_final_result(random_1, random_2, random_5)
def get_final_result(random_1, random_2, random_5):
final_list = []
if random_1 in random_5 and random_2 in random_5:
final_list.append(random_5)
result = quchong(final_list)
if result:
if len(result[0]) == 5:
final_result.append(result)
def quchong(list_data):
list = []
for i in list_data:
if i not in list:
list.append(i)
return list
if __name__ == '__main__':
start_time = time.time()
global final_result
final_result = []
get_random15()
final_result = quchong(final_result)
print("共%d个符合题意的列表" % len(final_result))
print("分别是:%s" % final_result)
end_time = time.time()
used_time = end_time - start_time
print()
print("本次程序用时:{}".format(time.strftime('%H(小时):%M(分钟):%S(秒)', time.gmtime(used_time))))Conclusion
The Monte Carlo‑based Python solution successfully generates all 5‑element subsets that contain a consecutive pair, but the runtime grows quickly with larger iteration counts or value ranges, highlighting a trade‑off between completeness and performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
