Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of random numbers
#1
Hi,
i have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random
 
 
def newlist(x):
    y = []
    for i in x:
        if i not in y:
            y.append(i)
    return y
 
 
a = []
 
for i_nd in range(0, 1000):
    n = random.randint(0, 100)
    a.append(n)
 
 
print(newlist(a))
and for some reason, even though i indicated in for i_nd in range(0, 1000): a range from 0 to 1000 - it only gives me 100 numbers....(when what i want is 1,000 numbers in the range from 0 to 100)

do you know what can it be ?
Reply
#2
It is not possible to have 1000 unique numbers in the range from 0 to 100.
Reply
#3
It is because the call to newlist() removes duplicates.
Reply
#4
You have a list of 1000 random numbers, before you pass that list to your function. Your function is then striping out all the duplicates, which leaves all the numbers from 0 to 100.

If you put in y.sort() before the return, you see what I mean:

1
2
3
4
5
6
7
def newlist(x):
    y = []
    for i in x:
        if i not in y:
            y.append(i)
    y.sort()
    return y
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
#5
You shufle 20 decks of standard playing cards. You remove all the duplicate cards so there are only one of each. How many cards remain?

Do you see how this relates to your question?
Reply
#6
yea i know (one deck),

but the thing is i do want duplicates...
okay here i fixed it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random
 
 
def newlist(x):
    y = []
    for i in x:
        y.append(i)
    y.sort()
    return y
 
 
a = []
 
for i_nd in range(0, 1000):
    n = random.randint(0, 100)
    a.append(n)
 
print(len(a))
print(newlist(a))
thank you everyone
heyy where did my last message went ?

okay i'll write it again,

i have the current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import random
from collections import Counter
 
 
def newlist(x):
    y = []
    for i in x:
        y.append(i)
    y.sort()
    return y
 
 
a = []
 
for i_nd in range(0, 1000):
    n = random.randint(0, 100)
    a.append(n)
 
b = a
b.sort()
 
my_List = dict(Counter(b))
 
print(my_List)
 
print(len(a))
print(newlist(a))
Output:
{0: 7, 1: 14, 2: 6, 3: 13, 4: 7, 5: 10, 6: 4, 7: 11, 8: 12, 9: 7, 10: 14, 11: 9, 12: 7, 13: 7, 14: 8, 15: 11, 16: 4, 17: 6, 18: 16, 19: 10, 20: 7, 21: 16, 22: 7, 23: 6, 24: 9, 25: 7, 26: 11, 27: 12, 28: 9, 29: 10, 30: 12, 31: 4, 32: 8, 33: 7, 34: 11, 35: 9, 36: 8, 37: 17, 38: 7, 39: 11, 40: 7, 41: 9, 42: 12, 43: 11, 44: 10, 45: 13, 46: 12, 47: 11, 48: 15, 49: 16, 50: 12, 51: 7, 52: 10, 53: 13, 54: 18, 55: 5, 56: 7, 57: 7, 58: 7, 59: 11, 60: 11, 61: 8, 62: 14, 63: 9, 64: 16, 65: 4, 66: 8, 67: 12, 68: 11, 69: 17, 70: 11, 71: 12, 72: 6, 73: 9, 74: 10, 75: 11, 76: 7, 77: 11, 78: 12, 79: 4, 80: 9, 81: 9, 82: 5, 83: 16, 84: 12, 85: 15, 86: 12, 87: 7, 88: 13, 89: 8, 90: 13, 91: 15, 92: 14, 93: 8, 94: 6, 95: 16, 96: 7, 97: 7, 98: 11, 99: 6, 100: 4}
now, suppose i want to order the output so the order of the numbers will be by their prevalence...and not by the numeric order as it is at the moment...

what shall be done ?
Reply
#7
Read the Counter documentation
Reply
#8
I'm struggling to understand why you have so much code.

What does your code do, that this stripped down version does not do (sorting, aside)?

1
2
3
4
5
6
7
8
9
10
11
import random
from collections import Counter
 
a = []  # holds a list of 1000 numbers; 0 to 100
 
 
for i_nd in range(0, 1000):
    n = random.randint(0, 100)
    a.append(n)
 
number_dict = dict(Counter(a)) # renamed
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
yea (rob101), you're right, i stripped it down as well, no need for the function above...

Dean - i read the documentation on python.org, not much there,
anyway - i read/searched some more info on other sites but haven't found something useful... Bonkself
Reply
#10
It can be reduced to
1
2
3
4
5
import random
from collections import Counter
 
counter = Counter(random.randint(0, 100) for _ in range(1000))
print(counter.most_common())
Output:
[(54, 18), (64, 18), (74, 16), (1, 16), (89, 15), (96, 15), (37, 14), (55, 14), (30, 13), (63, 13), (99, 13), (3, 13), (24, 13), (4, 13), (12, 12), (46, 12), (47, 12), (22, 12), (65, 12), (13, 12), (52, 12), (88, 12), (10, 12), (21, 12), (18, 12), (0, 12), (23, 12), (90, 12), (41, 11), (82, 11), (50, 11), (81, 11), (83, 11), (92, 11), (5, 11), (51, 11), (95, 11), (40, 11), (97, 11), (68, 11), (16, 11), (2, 11), (62, 11), (73, 11), (60, 11), (98, 11), (9, 11), (67, 10), (36, 10), (43, 10), (100, 10), (15, 10), (61, 10), (86, 10), (11, 10), (38, 10), (93, 9), (84, 9), (48, 9), (7, 9), (49, 9), (35, 9), (77, 9), (8, 9), (34, 9), (56, 9), (94, 9), (42, 9), (58, 8), (28, 8), (31, 8), (78, 8), (32, 8), (29, 8), (87, 8), (6, 8), (44, 8), (33, 8), (25, 8), (75, 8), (14, 8), (70, 8), (39, 8), (66, 7), (72, 7), (20, 7), (26, 7), (69, 7), (59, 6), (53, 6), (27, 6), (19, 6), (91, 6), (80, 6), (85, 5), (71, 5), (45, 5), (79, 5), (17, 4), (76, 4), (57, 2)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Entry field random pull from list, each return own line Bear1981 6 796 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 20,965 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 1,597 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 1,894 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 7,137 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Remove numbers from a list menator01 4 3,688 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 2,821 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Divide a number by numbers in a list. Wallen 7 10,974 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  producing numbers out of a list bouraque7878 10 5,755 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  How to change odd to even numbers in the list? plumberpy 8 5,537 Aug-08-2021, 11:07 AM
Last Post: plumberpy

Forum Jump:

User Panel Messages

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