Skip to content
Untitled Python workspace
# Start coding here...
import random
import openpyxl
# Generate 480 random numbers between 0 and 1 with 20% probability of being 0
random_numbers = random.choices([1, 0], k=480, weights=[0.2, 0.8])
print(random_numbers)
# Create a new Excel workbook
workbook = openpyxl.Workbook()
# Get the active worksheet in the workbook
worksheet = workbook.active
# Write the random numbers to the first column of the worksheet
for i, number in enumerate(random_numbers, start=1):
cell = worksheet.cell(row=i, column=1)
cell.value = number
# Save the workbook to a file
workbook.save("random_numbers.xlsx")
import openpyxl
import random
# Create a new Excel workbook
workbook = openpyxl.Workbook()
# Get the active worksheet in the workbook
worksheet = workbook.active
# Generate 460 random numbers between 0 and 1 with 25% probability of being 1
random_numbers = random.choices([0, 1], k=460, weights=[0.75, 0.25])
# Write the random numbers to columns in the worksheet
for i, number in enumerate(random_numbers, start=1):
column = i % 20 # Divide the index by 20 and use the remainder as the column number
if column == 0: # If the remainder is 0, set the column number to 20
column = 20
cell = worksheet.cell(row=i//20+1, column=column) # Divide the index by 20 and use the quotient as the row number
cell.value = number
# Save the workbook to a file
workbook.save("random_numbers.xlsx")