Python Forum

Full Version: find random numbers that are = to the first 2 number of a list.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hello,

I have a list. start_list = [1,2] and a random_list[]

My goal is to find 4 random numbers where the 2 first numbers are the same as my start_list.

I came up with that code:

import random

random_list = []
start_list = [1,2]

for i in range(4):
    num1 = random.randint(1,2)
    random_list.append(num1)

    if num1 == start_list: 
        print ("RL", num1) 
    else: 
        print ("SL", start_list) 

print(random_list)
Output:
SL [1, 2] SL [1, 2] SL [1, 2] SL [1, 2] [1, 2, 2, 1]
Why is it not returning RL [1.2] ? After all 1,2 are in the random_list and begins by 1 and 2?

The thing is i have to click on run to get a new series of random numbers. Is there a way i can make the process automatic so it looks automatically for my 4 random numbers where the number 1 and 2 are in order of the list, just like in the output.

Probably using sort() but i cant seem to find the exact combination of words on google to get the answer i want and english is not my native language.

Thank you
int == list will never be True.

I do not understand what you are trying to do. If you want to make a list len(N) that starts with [a, b] why not make a list len(N-2) and append to [a, b]. Why are you generating N random numbers?
new_list = start_list + sorted([randint(3,MAX) for _ in range(2)])
num1 is never going to be == start_list because num1 is a 'class int(object) instance' and start_list is a 'class list(object) instance'.

So maybe what you're looking to do, is this?

import random

start_list = [1, 2]

for i in range(4):
    random_list = []
    while len(random_list) < 2:
        num1 = random.randint(1, 2)
        random_list.append(num1)
    if random_list == start_list:
        print("RL", random_list)
    else:
        print("SL", start_list)
Well there is always:

import random
 
random_list = []
start_list = [1,2]
 
for i in range(4):
    num1 = random.randint(1,2)
    random_list.append(num1)
 
    for i in start_list:
        if i == num1: 
            print ("RL", random_list) 
        else: 
            print ("LL", start_list) 
 
print(random_list)
Than RL will come up but i have to check the first 2 numbers if (1,2) are true. Its checking the whole serie of numbers but i only one him to check the first 2.
Rob thats not what i am looking for neither what denystad propose in post 2.

Let me rephrase the problem.

Suppose you ask a computer to generate 4 numbers. But you want those numbers to begin with (1,2) or (3,6)

The computer will generate all the random series of numbers with 4 numbers and it will stop when the first 2 numbers will be 1 and 2 or 3 and 6 or any specific numbers you have in mind.

My guess is that the more you ask for numbers the longer it should take before the computer come up with the right set. But lets start with something small 4 numbers.

The computer generate series:
(2,2,3,4) than check is 1,2 are first in the set. no! next serie
(1,2,3,3) is 1 and 2 are first in the serie? Yes! the computer return (1,2,3,3)

Is it more clear?
TY
Your description does not match your code. Can you describe what you are trying to do. What do you start with, and what do you want as a result.
answer in post 5, unless you meant that post 5 is not clear
(Apr-03-2023, 02:33 PM)Frankduc Wrote: [ -> ]Is it more clear?

Yes; that's clearer.

Is this closer?

 import random

start_list = [1, 2]
for i in range(40):
    random_list = []
    while len(random_list) < 4:
        num1 = random.randint(1, 4)
        random_list.append(num1)
    check = random_list[0:2]
    if check == start_list:
        print("Found")
        print("RL", random_list)
        print()
Is the problem that you have a bunch of random lists and you are trying to find lists that match the start pattern, or are you trying to generate random lists that match the start problem. These are totally different problems and there are totally different approaches for solving.

This is my solution to the first problem (finding lists that match start)
start_list = [1, 2]
MAX = 100

# Since I don't have a bunch of random lists to test, I will make some and see how long it takes to find a match.
for attempt in range(1, 10000000000000):
    random_list = [randint(1, MAX) for _ in range(4)]
    if random_list[:2] == start_list:
        print(attempt, ":", random_list)
        break
This is my approach to solving the second problem (making list len(4) that starts with starting pattern.
start_list = [1, 2]
MAX = 100
random_list = start_list +  [randint(1, MAX) for _ in range(2)]
print(random_list)
good question Dean and thats where i am not sure last code from rob works. Because if you compare found list numbers to random_list they dont match.

Like i said you are asking the computer to throw at you 4 random numbers. (1,1,4,3)
You than compare those 4 numbers to your list, the start_list =[1,2]

If the computer 4 random numbers are starting with [1,2] you have a match.
In the case of (1,1,4,3) its not a match than the computer generate another 4 numbers and check again if it fit the condition of the list. (1,2) at the beginning.

What i am interested in, is to analyze what subsequent numbers the computer will generate after 1 and 2. But they have to be part of the original serie and not be added afterward.

I hope my explanation are a bit better.
Pages: 1 2 3