Python Forum
find random numbers that are = to the first 2 number of a list.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find random numbers that are = to the first 2 number of a list.
#1
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
Reply
#2
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)])
Reply
#3
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)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
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.
Reply
#5
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
lothar likes this post
Reply
#6
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.
Reply
#7
answer in post 5, unless you meant that post 5 is not clear
Reply
#8
(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()
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
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)
Frankduc likes this post
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,720 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 574 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 463 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  find the sum of a series of values that equal a number ancorte 1 505 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Program to find Mode of a list PythonBoy 6 1,113 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Delete strings from a list to create a new only number list Dvdscot 8 1,552 May-01-2023, 09:06 PM
Last Post: deanhystad
  List of random numbers astral_travel 17 2,723 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,238 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Remove numbers from a list menator01 4 1,344 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,529 Oct-23-2022, 11:13 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020