meag_a
meag_a

Reputation: 27

Creating Unsorted List in range 100,000 to 1000,000

I am looking to create a function that would return an unsorted list of 20 numbers in the range 100 to 1,000 (in thousands) and need not be unique. Further, I am looking for them to be multiples of 25.

In the next step, I am looking to sort them from smallest to largest. I am looking for help on how to insert the restrictions of max 20 random UNSORTED numbers divisible by 25:

def bookinventory(i, j):
    import random
    booknumber = [x for x in range(i, j)]
    random.shuffle(booknumber)
    print(booknumber)

def main():
    books = bookinventory(100000, 1000000)
    print(books)

if __name__ == '__main__':
    main()

Upvotes: 1

Views: 774

Answers (4)

ITSME
ITSME

Reputation: 36

I made a program to get your desired output

import random 
final_list = []
final_list_set = set(final_list)
final_list_set_len = len(final_list_set)
while final_list_set_len < 20:
    final_list_set = set(final_list)
    final_list_set_len = len(final_list_set)
    num = random.randint(100,1000)
    if (num%25 != 0):
        continue
    else:
        final_list.append(num)
        continue
final_list = list(final_list_set)
final_list.sort(reverse = False)
print("The list is",final_list)
print("The length of the list is",len(final_list))

I added the last line in code so that you can verify the number of elements in the list. I hope this is useful :)

Upvotes: 0

Tomerikoo
Tomerikoo

Reputation: 19414

To limit to 20 elements you can do:

booknumber = [random.randint(i, j) for _ in range(20)]

To take only 20 divisible-by-25 elements you can use choices:

booknumber = random.choices([x for x in range(i, j+1) if x % 25 == 0], k=20)

This is for the general case where i and j are not known. If you know the numbers as in your example, you can use arithmetics to make things simpler:

booknumber = [random.randint(4000, 40000) * 25 for _ in range(20)]

Upvotes: 2

Kumpelinus
Kumpelinus

Reputation: 660

This will create 20 unique numbers between 100,000 and 1,000,000 divisible by 25:

def bookinventory(i, j):
    import random
    booknumbers = list(range(i, j + 1, 25))
    return sorted([random.choice(booknumbers) for _ in range(20)])

def main():
    books = bookinventory(100000, 1000000)
    sorted_books = sorted(books)
    print(sorted_books)


if __name__ == '__main__':
    main()

The main component of this is:

sorted([random.choice(booknumbers) for _ in range(20)])

Upvotes: 1

Lapis Rose
Lapis Rose

Reputation: 644

This will create a unsorted list of 20 non-unique integers ranging from 100,000 to 1,000,000 that are divisible by 25.

b = [x * 25 for x in random.choices(range(4000, 40000), k = 20)]

You can change your code like this:

def bookinventory(i,j):
    import random
    booknumber = [x * 25 for x in random.choices(range(int(i/25), int(j/25)), k = 20)]
    print(booknumber)
    return booknumber

def main():
    books = bookinventory(100000,1000000)
    sorted_books = sorted(books)
    print(sorted_books)


if __name__ == '__main__':
    main()

Upvotes: 1

Related Questions