- (double)simulateStockPriceWithInitialPrice:(double)initialPrice upCount:(int)upCount downCount:(int)downCount increasePercentage:(double)increasePercentage decreasePercentage:(double)decreasePercentage {
double currentPrice = initialPrice;
NSMutableArray<NSNumber *> *events = [NSMutableArray array];
//填充事件数组,包含upCount个涨停事件和downCount个跌停事件
for (int i = 0; i < upCount; i++) {
[events addObject:@(1)]; // 1代表涨停
}
for (int i = 0; i < downCount; i++) {
[events addObject:@(0)]; // 0代表跌停
}
//打乱事件数组的顺序
for (int i = events.count - 1; i > 0; i--) {
int j = arc4random_uniform(i + 1);
[events exchangeObjectAtIndex:i withObjectAtIndex:j];
}
NSLog(@"events = %@",events);
//根据打乱后的事件数组模拟价格变化
for (NSNumber *event in events) {
if ([event intValue] == 1) {
//涨停
currentPrice *= (1 + increasePercentage / 100.0);
} else {
//跌停
currentPrice *= (1 - decreasePercentage / 100.0);
}
}
return currentPrice;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
double initialPrice = 1.0;//原价
int upCount = 10;//涨停次数
int downCount = 10;//跌停次数
double increasePercentage = 10.0; // 涨停比例
double decreasePercentage = 10.0; // 跌停比例
double finalPrice = [self simulateStockPriceWithInitialPrice:initialPrice upCount:upCount downCount:downCount increasePercentage:increasePercentage decreasePercentage:decreasePercentage];
NSLog(@"The final stock price after %d random ups and %d downs is: %.2f", upCount, downCount, finalPrice);
NSLog(@"经过%d次随机上涨和%d次下跌后的最终股价为: %.2f", upCount, downCount, finalPrice);
}