for循环

时间:2015-09-27 14:27:38

标签: c++ for-loop random

有很多类似的问题,但我无法找到问题的答案。我正在编写自己的随机数生成器(用于学习目的)。我在这里粘贴代码的重要部分:

long rng(long x0, long c, long p)
{
    long x = (c*x0) % p;
    return x;
}

main()函数中,我迭代此rng()

long x2 = 37951;                    // initialize the seed
long c = 69069; long p = pow(2,32); // initialize c and p

for (int i=1; i<=n; i++) {
    long x1 = rng(x2,c,p);
    long x2 = rng(x1,c,p);
    cout << x1 << "\t" << x2 <<endl;
}

之后我想生成极坐标,我需要在每个循环中生成2个随机数。如您所见,种子只被初始化一次,但我得到的结果是:

2621237619  504678423
2621237619  504678423
2621237619  504678423
2621237619  504678423
2621237619  504678423

如果我只生成x2(带long x2 = rng(x1,c,p);),我会在每个循环中得到不同的结果。有人可以告诉我为什么吗?

2 个答案:

答案 0 :(得分:0)

这是因为用于计算x2的{​​{1}}未被更新,因为它被定义为局部变量。

试试这个:

x1

注意:如果#include <iostream> #include <cmath> using std::cout; using std::endl; long rng(long x0, long c, long p) { long x = (c*x0) % p; return x; } int main(void) { long x2 = 37951; // initialize the seed long c = 69069; long p = pow(2,32); // initialize c and p int n = 5; for (int i=1; i<=n; i++) { long x1 = rng(x2,c,p); x2 = rng(x1,c,p); cout << x1 << "\t" << x2 <<endl; } return 0; } 的大小为4字节(32位)或更小,则此代码不会正常工作。

答案 1 :(得分:0)

因为你一次又一次地传递x2 x1,而x1被声明在外部循环而不在内部,你将x2传递给x2(本地到循环),但是因为你再次在这里声明x2 = 37951但是当每个循环结束时这超出了范围。所以基本上你每次x1传递long x2 = 37951; // initialize the seed long c = 69069; long p = pow(2, 32); // initialize c and p for (int i = 1; i <= n; i++) { long x1 = rng(x2, c, p); //long x2 = rng(x1,c,p) will declaring new variable every time //and this value will go out of scope when loop ends x2 = rng(x1, c, p); // reusing same x2 again so that it will hold value for next time cout << x1 << "\t" << x2 << endl; }

UIView *superview = self.view;
// Do any additional setup after loading the view, typically from a nib.
    self->closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self->closeBtn.frame = CGRectMake(260, 100, 50, 28);
    self->closeBtn.layer.cornerRadius = 4;
    self->closeBtn.layer.borderWidth = 1;
    self->closeBtn.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
    [self->closeBtn setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
    self->closeBtn.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
    [self->closeBtn setTitle:@"Done" forState:UIControlStateNormal];
    [self->closeBtn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0]];
    [self.view addSubview:self->closeBtn];
    [self->closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    self->closeBtn.translatesAutoresizingMaskIntoConstraints = NO;

    NSLayoutConstraint * c_1 =[NSLayoutConstraint
                               constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterX
                               relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
                               NSLayoutAttributeCenterX multiplier:1.0 constant:-7.5f];


    NSLayoutConstraint * c_2 =[NSLayoutConstraint
                               constraintWithItem:self->closeBtn attribute:NSLayoutAttributeCenterY
                               relatedBy:NSLayoutRelationEqual toItem:superview attribute:
                               NSLayoutAttributeCenterY multiplier:1.85f constant:0.0f];


    NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:self->closeBtn
                                                                attribute:NSLayoutAttributeWidth
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:0
                                                               multiplier:1.0
                                                                 constant:50];
    NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:self->closeBtn
                                                                attribute:NSLayoutAttributeHeight
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:0
                                                               multiplier:1.0
                                                                 constant:28];
    [self.view addConstraints:@[c_1,c_2]];
    [self->closeBtn addConstraints:@[equal_w,equal_h]];