Python random diamond placement on a grid

2025-hsc-se-q26 · Code Response · 5 marks

Source: NESA 2025 HSC Software Engineering HSC Q26

Question

As part of a computer game, a program is needed to randomly place 15 diamonds on a 5 x 5 grid. Once the locations of the diamonds have been generated, the program outputs the 5 x 5 grid, with the location of each diamond signified by an X.

An example of the output required is shown below.

| X |   | X |   | X |
|   | X |   |   | X |
|   | X | X | X |   |
| X |   | X |   |   |
| X | X | X | X | X |

Using Python, write a program that achieves this task.

Response

Reveal answer
import random

grid = [[" " for column in range(5)] for row in range(5)]
positions = random.sample(range(25), 15)

for position in positions:
    row = position // 5
    column = position % 5
    grid[row][column] = "X"

for row in grid:
    print("| " + " | ".join(row) + " |")

Marking rubric

MarksDescription
5Provides correct Python that randomly places 15 diamonds on a 5 x 5 grid and outputs the grid.
4Provides mostly correct Python with minor errors.
3Generates or stores diamond locations and attempts to output a grid.
2Shows partial use of arrays, loops or random number generation.
1Shows some relevant Python programming knowledge.

Explanation

random.sample avoids duplicate positions. Integer division and modulo convert a single position number into row and column indexes.

Metadata

Submitter
Seed data
Created
2026-05-02
Status
published
Syllabus
y12-project-design-construct-implement-solution
Tags
Python random arrays grids game development