1

I want to construct a list of 100 randomly generated share prices by generate 100 random 4-letter-names for the companies and a corresponding random share price.

So far, I have written the following code which provides a random 4-letter company name:

import string
import random

def stock_generator():
    return ''.join(random.choices(string.ascii_uppercase, k=4))

stock_name_generator()

# OUTPUT
'FIQG'

But, I want to generate 100 of these with accompanying random share prices. It is possible to do this while keeping the list the same once it's created (i.e. using a seed of some sort)?

codemachino
  • 103
  • 9
  • Pay attention to the fact that you define a name for the function, then you call a function with a different name. Then, check my answer :) – Girolamo Jan 25 '22 at 11:32

4 Answers4

2

I think this approach will work for your task.

import string
import random

random.seed(0)

def stock_generator():
    return (''.join(random.choices(string.ascii_uppercase, k=4)), random.random())

parse_result =[]
n=100
for i in range(0,n):
    parse_result.append(stock_generator())

print(parse_result)
1
import string
import random


random.seed(0)


def stock_generator(n=100):
    return [(''.join(random.choices(string.ascii_uppercase, k=4)), random.random()) for _ in range(n)]


stocks = stock_generator()

print(stocks)

You can generate as many random stocks as you want with this generator expression. stock_generator() returns a list of tuples of random 4-letter names and a random number between 0 and 1. I image a real price would look different, but this is how you'd start.

random.seed() lets you replicate the random generation.

Edit: average stock price as additionally requested

average_price = sum(stock[1] for stock in stocks) / len(stocks)

stocks[i][1] can be used to access the price in the name/price tuple.

Tobi208
  • 1,306
  • 10
  • 17
  • Thanks @Tobi208 ! I have implemented this method and I want to find the mean of the share prices. Is it possible to do this since the variable `stocks` is a list of lists? – codemachino Jan 25 '22 at 11:44
  • 1
    @codemachino no problem and sure! I edited my answer, does that solve it? – Tobi208 Jan 25 '22 at 11:51
1

You can generate a consistent n random samples by updating the seed of random before shuffle. Here is an example on how to generate these list of names (10 samples):

import random, copy

sorted_list = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
i = 0 #counter
n = 10 # number of samples
l = 4 # length of samples

myset = set()
while i < n:
    shuffled_list = copy.deepcopy(sorted_list)
    random.seed(i)
    random.shuffle(shuffled_list)
    name = tuple(shuffled_list[:l])
    if name not in myset:
        myset.add(name)
        i+=1
print(sorted([''.join(list(x)) for x in myset]))
# ['CKEM', 'DURP', 'GQXO', 'JFWI', 'JNRX', 'MNSV', 'OAXS', 'TIFX', 'VLZS', 'XYLK']

Then you can randomly generate n number of prices and create a list of tuples that binds each name to a price:

names = sorted([''.join(list(x)) for x in myset])
int_list = random.sample(range(1, 100), n)
prices = [x/10 for x in int_list]

names_and_prices = []
for name, price in zip(names,prices):
    names_and_prices.append((name,price))

# [('CKEM', 1.5), ('DURP', 1.7), ('GQXO', 6.5), ('JFWI', 7.6), ('JNRX', 0.9), ('MNSV', 8.9), ('OAXS', 5.0), ('TIFX', 9.6), ('VLZS', 1.4), ('XYLK', 3.8)]

b-fg
  • 3,959
  • 2
  • 28
  • 44
0

Try:

import string
import random



def stock_generator(num):
    names = []

    for n in range(num):
        x = ''.join(random.choices(string.ascii_uppercase, k=4))
        names.append(x)

    return names    
print(stock_generator(100))


Each time you will call the function stock_generator using a number of your choice as parameter, you'll generate the stock name you need.

Girolamo
  • 326
  • 3
  • 11