Help with class, std::cin and random number generator

I am to stupid :)
I would like to give the class a "seed" which it use for the random number generator.
Can you help me.
Please rewrite the code, because I don't think I will understand if you just come with an advise :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<random>


class MSRNG
{
public:
    MSRNG() = default;
    MSRNG(double b);
    ~MSRNG();
    double Get_rand(double seed);
    double Set_seed();
    void print();
private:
    double random_val = 0;
    double seed = 10;
};

MSRNG::MSRNG(double result)
{
    random_val = result;
}

double MSRNG::Get_rand(double seed) {
    std::minstd_rand GetLCG;
    GetLCG.seed(seed);
    double result = -6;
    for (int i = 0; i < 12; ++i)
    {
        //std::cout << GetLCG() << '\n';
        result += GetLCG() / (double)(GetLCG.max());
    }
    return result;
}


double MSRNG::Set_seed() {
    std::cout << "what seed?" << std::endl;
    std::cin >> seed;
    return seed;
}

MSRNG::~MSRNG()
{
}

void MSRNG::print() {
    std::cout << "a random var " << random_val << std::endl;
}

int main() {
        
    MSRNG random;
    random.Set_seed();
    random.Get_rand(1);
    random.print();
    std::cin.get();

}
Please rewrite the code, because I don't think I will understand if you just come with an advise
You will learn much better if you make the changes yourself. Coding is like playing football - you have to practice.

It isn't printing anything because print() prints random_val, but you never set it. Add random_val = result;jus before line 33 to fix this.

After you make that change, you'll notice that you get the same value no matter what seed you use. That's because you seed the random number generator at line 26 with the seed parameter to the Get_rand() (the parameter declared at line 24). This is a different variable from the seed member declared at line 16. To fix this with the least code, change Get_rand() at lines 11 and 24 so it takes no parameters. Then change the call at line 55 so it doesn't pass a parameter.

While this will work, it's still not a great solution because if you modify your code to call Get_rand() more than once, you'll find that it always produces the same value. That's because you seed the random number generator each time you call Get_rand(). To fix that problem, you need to seed the random number generator in Set_seed(). But you still have to call it in Get_rand(). The solution to this is to make the random number generator a private member of the class.

There are other improvements that could be made, but I'll stop here for now. See if you can make these changes.
Hi dhayden

Thank you so much for your time :)
I rewrote the code, do you have any advise for this code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include<iostream>
#include<random>

class RandomNumber
{
public:
	RandomNumber();
	~RandomNumber();
	int GetSeed();
	double GetLCG();
	void print();
private:
	int seed = 1;
	double TheNumber;
};

RandomNumber::RandomNumber()
{
}

RandomNumber::~RandomNumber()
{
}

int RandomNumber::GetSeed() {
	int a;
	std::cin >> a;
	seed = a;
	return seed;
}

double RandomNumber::GetLCG() {
	std::minstd_rand GetLCG;
	GetLCG.seed(seed);
	double result = -6;
	for (int i = 0; i < 12; ++i)
	{
		//std::cout << GetLCG() << '\n';
		result += GetLCG() / (double)(GetLCG.max());
	}
	TheNumber = result;
	return TheNumber;
}

void RandomNumber::print() {
	std::cout << "the seed is: " << seed << std::endl;
	std::cout << "and the N(0,1) number is : " << TheNumber << std::endl;
}
int main() {

	RandomNumber val1;
	val1.GetSeed();
	val1.GetLCG();
	val1.print();
	std::cin.get();
	std::cin.get();
}
The code looks good for what it does. I'd do it differently though.
- Have the class do the computation only and move the I/O to the main program. This makes the class much more reusable.
- Move the random number generator into the class itself.
- Add a setSeed() method that sets the seed in the rng.
- Have one constructor with a parameter (the seed) that takes a default. The constructor calls setSeed()
- Don't store the seed or result of GetLCG() in the class. Let the caller do that if they need to.

Putting it all together:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<random>

class RandomNumber
{
  public:
    RandomNumber(double seed);
    void setSeed(double seed);
    double GetLCG();
  private:
      std::minstd_rand rng;
};

RandomNumber::RandomNumber(double seed)
{
    setSeed(seed);
}

void
RandomNumber::setSeed(double seed)
{
    rng.seed(seed);
}

double
RandomNumber::GetLCG()
{
    double result = -6;
    for (int i = 0; i < 12; ++i) {
	//std::cout << rng() << '\n';
	result += rng() / (double) (rng.max());
    }
    return result;
}

int
main()
{
    double seed;
    std::cout << "Please enter a seed value: ";
    std::cin >> seed;

    RandomNumber val1(seed);

    std::cout << "the seed is: " << seed << std::endl;
    std::cout << "and the N(0,1) number is : " << val1.GetLCG() << std::endl;
}

Thank you so much, dhayden :)

Topic archived. No new replies allowed.