Skip to content

Instantly share code, notes, and snippets.

@VegaFromLyra
Created June 6, 2015 23:00
Show Gist options
  • Save VegaFromLyra/7e48b1232c7eb82a50b7 to your computer and use it in GitHub Desktop.
Save VegaFromLyra/7e48b1232c7eb82a50b7 to your computer and use it in GitHub Desktop.
Random weighted stock
using System;
using System.Collections.Generic;
// generateRandomStock: Generate a random stock weighted according to the value of the stock
//
// key: Stock Name
// Value: The number of times this stock is traded in a given day
namespace generateRandomStock
{
public class Program
{
static Dictionary<string, int> tradingMap = new Dictionary<string, int>();
static List<string> allStocks = new List<string>();
public static void Main(string[] args)
{
tradingMap.Add("msft", 233);
tradingMap.Add("goog", 1052);
tradingMap.Add("appl", 10678);
init();
var stockToBuy = generateRandomStock();
Console.WriteLine("Get {0} stock", stockToBuy);
}
static void init() {
int totalStockCount = 0;
foreach (var stock in tradingMap.Keys) {
totalStockCount += tradingMap[stock];
}
foreach (var stock in tradingMap.Keys) {
double percentage = ((double) tradingMap[stock] / (double) totalStockCount) * 100;
int entries = (int)(Math.Ceiling(percentage));
for (int i = 0; i < entries; i++) {
allStocks.Add(stock);
}
}
}
static string generateRandomStock() {
var random = new Random();
var index = random.Next(allStocks.Count);
return allStocks[index];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment