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


Messages In This Thread
find random numbers that are = to the first 2 number of a list. - by Frankduc - Apr-03-2023, 01:58 PM

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,911 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 631 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 526 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  find the sum of a series of values that equal a number ancorte 1 558 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Program to find Mode of a list PythonBoy 6 1,230 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Delete strings from a list to create a new only number list Dvdscot 8 1,688 May-01-2023, 09:06 PM
Last Post: deanhystad
  List of random numbers astral_travel 17 2,879 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,310 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Remove numbers from a list menator01 4 1,442 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,644 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