我正在用 Python 编写一个 LCG 函数,我将使用它来进行蒙特卡罗类型的硬币翻转和生成运行模拟。我面临的问题是,当我生成一个随机数列表时,这些数字的模式会使得赔率和偶数交替出现。我不知道这是 LCG 函数本身的属性还是我生成数字的错误。
这是我的代码:
def seedLCG(initVal):
global rand
rand = initVal
def lcg():
a = 1140671485
c = 128201163
m = 2**24
global rand
rand = (a*rand + c) % m
return rand
seedLCG(1)
for i in range(10):
print lcg()
Run Code Online (Sandbox Code Playgroud)
返回值:
10581448
11595891
1502322
14136437
11348076
1403015
9622582
11013417
11529808
15836891
Run Code Online (Sandbox Code Playgroud)
我假设我不需要担心溢出和大小,因为 int 和 long 会根据 Python 的需要进行互换。
我目前有一个具有消息传递功能的应用程序.它允许用户互相聊天.目前,消息从下往上显示(我能够通过旋转表格和单元格来完成此操作).现在要区分发送者和接收者我使用不同的彩色文本.
我想使用气泡图像作为消息的背景,以便应用程序看起来更像iMessage.我知道有一点可以调整图像大小,但我无法理解这一点.我需要一种特殊的形象吗?如果是这样,我如何调整该图像的大小以使其适合文本然后将其作为背景?
在此先感谢您的帮助.
-EDIT-代码我用来生成带有气泡的单元格:
[self.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:20]];
[self.textLabel setNumberOfLines:0];
CGSize size = [message.message sizeWithFont:self.textLabel.font];
[self.textLabel setFrame:CGRectMake(690.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height)];
UIImage *bubble = [[UIImage imageNamed:@"aqua.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *bubbleView = [[UIImageView alloc] initWithFrame:CGRectMake(704 - (size.width + 25.0f), 2.0f, size.width + 25.0f, size.height + 15.0f)];
bubbleView.image = bubble;
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.backgroundView = bubbleView;
Run Code Online (Sandbox Code Playgroud)