Python Forum
How to Generate and Print An Array with Random Numbers in Python in the
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Generate and Print An Array with Random Numbers in Python in the
#1
I am a beginner in Django. I am trying to solve a Bingo card game problem, where I have to generate an array and print the random numbers without any duplication.

I have to print this output:

     W      O      R      L      D 
W   10     93     85     14     18    
O   24     96     88     29     23    
R   36     33     99     90     31     
L   46     48     92     95     43     
D   59     76     51     72     58   
It's a bingo game. The card gets filled with random Numbers. I have tried the following codes:

import random

class Card:
    def __init__(self):
        self.b = random.sample(range(1,100),5)
        self.i = random.sample(range(1,100),5)
        self.n = random.sample(range(1,100),5)
        self.g = random.sample(range(1,100),5)
        self.o = random.sample(range(1,100),5)

        print(self.b)
        print(self.i)
        print(self.n)
        print(self.g)
        print(self.o)
The codes don't deliver the desired output.
Reply
#2
You need to take one sample of 25 items if you want them all to be unique. You can split them into groups of five with slices, like b = bingo[:5] and i = bingo[5:10].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-18-2019, 01:18 AM)ichabod801 Wrote: You need to take one sample of 25 items if you want them all to be unique. You can split them into groups of five with slices, like b = bingo[:5] and i = bingo[5:10].

Would you please send me the codes? Because I am new in Python. So, I cannot fully understand what you are saying. If you send the codes, it will be much easier for me to comprehend.
Reply
#4
I feel very stupid. When I learned Python it took quite a bit to learn basics and only after that I started writing classes. Now it seems that 'new' means upside down: ability to write classes (and applications) but know next to nothing about basics.

ichabod801 provided algorithm. It's four rows of Python code.

>>> import random
>>> draw = random.sample(range(1, 51), 25)
>>> for i in range(0, 25, 5):
...     print(*draw[i:i+5])
...
17 16 18 8 21
3 38 48 2 49
35 14 11 41 25
40 9 29 1 15
44 50 39 45 31
EDIT: oops. I noticed that this is under 'Homework'. Admins feel free to delete code if deemed appropriate.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
You are not doing Bingo cards in this assignment. For one thing, Bingo cards do not have the letters down the side, only across the top. Second, B is in the range 1-15 inclusive, I in range 16-30 inclusive, N (31:45) G (46:60) O (61:75).

So, in your example, W 44 should not exist.

Your assignment might have different rules, but if this truly is a "create a Bingo card" you need to consider the above. Pick 5 numbers in the range 1-15, etc.
Reply
#6
(Sep-18-2019, 01:02 PM)perfringo Wrote: I feel very stupid. When I learned Python it took quite a bit to learn basics and only after that I started writing classes. Now it seems that 'new' means upside down: ability to write classes (and applications) but know next to nothing about basics.

ichabod801 provided algorithm. It's four rows of Python code.

>>> import random
>>> draw = random.sample(range(1, 51), 25)
>>> for i in range(0, 25, 5):
...     print(*draw[i:i+5])
...
17 16 18 8 21
3 38 48 2 49
35 14 11 41 25
40 9 29 1 15
44 50 39 45 31
EDIT: oops. I noticed that this is under 'Homework'. Admins feel free to delete code if deemed appropriate.

The code you provided doesn't have any function to eliminate the duplicate numbers. Nonetheless, thanks for providing me with the codes. :)

I have found a solution from here.

    from random import randint
    
    class Card:
        def __init__(self):
            self.data1d = []
            for i in range(25):
                self.data1d.append(randint(1, 100))
    
            duplicate = True
            while duplicate == True:
                duplicate = False
                for j in range(len(self.data1d)):
                    for i in range(len(self.data1d)):
                        if i != j and self.data1d[i] == self.data1d[j]:
                            self.data1d[j] = randint(1, 100)
                            duplicate = True
    
            self.data = []
    
            for j in range(5):
                row = []
                for i in range(5):
                    row.append(self.data1d[j * 5 + i])
    
                self.data.append(row)
    
            letters = ['W', 'O', 'R', 'L', 'D']
            printText = ['      ', '']
            for i in letters:
                printText[0] += i + '     '
    
            for j in range(len(self.data)):
                text = letters[j] + '   '
                for i in self.data[j]:
                    if i < 10:
                        space = '  '
                    elif i < 100:
                        space = ' '
                    else:
                        space = ''
                    text += space + str(i) + '   '
    
                printText.append(text)
    
            for i in printText:
                print(i)
    
    c = Card()
However, I don't understand some of the lines of the codes. Would you please explain what is happening with these codes?
            duplicate = True
            while duplicate == True:
                duplicate = False
                for j in range(len(self.data1d)):
                    for i in range(len(self.data1d)):
                        if i != j and self.data1d[i] == self.data1d[j]:
                            self.data1d[j] = randint(1, 100)
                            duplicate = True
    
            self.data = []
    
            for j in range(5):
                row = []
                for i in range(5):
                    row.append(self.data1d[j * 5 + i])
    
                self.data.append(row)
Reply
#7
(Sep-18-2019, 02:57 PM)johnnynitro99293 Wrote: The code you provided doesn't have any function to eliminate the duplicate numbers.

How could one find duplicates in random.sample result which: “Return a k length list of unique elements chosen from the population sequence or set.”.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(Sep-18-2019, 02:38 PM)jefsummers Wrote: You are not doing Bingo cards in this assignment. For one thing, Bingo cards do not have the letters down the side, only across the top. Second, B is in the range 1-15 inclusive, I in range 16-30 inclusive, N (31:45) G (46:60) O (61:75). So, in your example, W 44 should not exist. Your assignment might have different rules, but if this truly is a "create a Bingo card" you need to consider the above. Pick 5 numbers in the range 1-15, etc.
According to our lecture sheet and teacher, this is a BINGO assignment.
Reply
#9
(Sep-18-2019, 03:09 PM)perfringo Wrote:
(Sep-18-2019, 02:57 PM)johnnynitro99293 Wrote: The code you provided doesn't have any function to eliminate the duplicate numbers.
How could one find duplicates in random.sample result which: “Return a k length list of unique elements chosen from the population sequence or set.”.
I was also a bit confused in the class. I told my teacher that random.sample returns unique elements, not any duplicate elements. However, he explained a theory that I couldn't understand.
Reply
#10
(Sep-18-2019, 04:12 PM)johnnynitro99293 Wrote: I was also a bit confused in the class. I told my teacher that random.sample returns unique elements, not any duplicate elements. However, he explained a theory that I couldn't understand.

Theory about what? With couple of rows of one can 'verify' that indeed there are unique elements. 'for million times take random sample of 25 items from range of 50, uniquify it with set to remove duplicates and check whether there is still 25 items'.

for i in range(1000000): 
    if len(set(random.sample(range(50), 25))) != 25: 
        print('Duplicate detected') 
        break 
else:                                  # no-break
    print('No duplicates detected') 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,369 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  random interger array question rpang 3 1,779 Nov-05-2022, 12:31 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 2,935 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Getting largest indices of array less than or equal to an array of numbers schniefen 5 2,583 Nov-02-2020, 08:14 PM
Last Post: schniefen
  Scaling random numbers within specific range schniefen 4 3,135 Oct-28-2019, 11:22 AM
Last Post: jefsummers
  python homework print the sum of a range of numbers from x to y kirito85 3 3,231 Oct-28-2018, 08:56 AM
Last Post: kirito85
  10x10 Array of Prime Numbers smfox2 1 2,503 Sep-03-2018, 12:36 AM
Last Post: ichabod801
  random numbers and stats cliffhop23 2 6,853 Feb-22-2018, 09:16 PM
Last Post: glidecode
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,053 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  NEED HELP Pseudo-Random Numbers Kongurinn 9 5,342 Oct-23-2017, 04:17 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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