RNG Function

It's been quite a while since I last programmed but trying to get back into it so I'm sure I'm missing something obvious but see below:
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
#include <iostream>
#include <random>

using std::default_random_engine;
using std::cout;

void genNPIs(default_random_engine toGen);

int main() {
	default_random_engine gen();
	genNPIs(gen);

	return 0;
}

void genNPIs(default_random_engine toGen) {
	for (unsigned int i = 0; i < 100; i++) {
		cout << toGen() << ' ';
		if (i % 9 == 0) {
			cout << '\n';
		}
	}

	return;
}


I am getting an error on line 11 - genNPIs(gen); - and for the life of me I can't figure out why. VS2019 is also not showing me the type of gen when I hover over the error and I've tried resetting settings but that hasn't helped.

EDIT: I've tried passing the gen object by address as well and am still not able to compile
Last edited on
Hello edge6768,

On line 10 I removed the () and it compiled and ran file in VS2019.

I believe the compiler is considering line 10 as a prototype.

"default_random_engine" is a type like "int". "gen" is the variable name.

I did make a change in the function: cout << (i % 7 ? ", " : "\n") << toGen(); and I get this output:

3499211612, 581869302, 3890346734, 3586334585, 545404204, 4161255391, 3922919429
949333985, 2715962298, 1323567403, 418932835, 2350294565, 1196140740, 809094426
2348838239, 4264392720, 4112460519, 4279768804, 4144164697, 4156218106, 676943009
3117454609, 4168664243, 4213834039, 4111000746, 471852626, 2084672536, 3427838553
3437178460, 1275731771, 609397212, 20544909, 1811450929, 483031418, 3933054126
2747762695, 3402504553, 3772830893, 4120988587, 2163214728, 2816384844, 3427077306
153380495, 1551745920, 3646982597, 910208076, 4011470445, 2926416934, 2915145307
1712568902, 3254469058, 3181055693, 3191729660, 2039073006, 1684602222, 1812852786
2815256116, 746745227, 735241234, 1296707006, 3032444839, 3424291161, 136721026
1359573808, 1189375152, 3747053250, 198304612, 640439652, 417177801, 4269491673
3536724425, 3530047642, 2984266209, 537655879, 1361931891, 3280281326, 4081172609
2107063880, 147944788, 2850164008, 1884392678, 540721923, 1638781099, 902841100
3287869586, 219972873, 3415357582, 156513983, 802611720, 1755486969, 2103522059
1967048444, 1913778154, 2094092595, 2775893247, 3410096536, 3046698742, 3955127111
3241354600, 3468319344


Any more than 7 and it would reach the right side of the screen an not look ver good.

If you do not want the large numbers you can add the code:
1
2
3
4
const int range_from = 0;
const int range_to = 201;

std::uniform_int_distribution<int>  distr(range_from, range_to);

I think I would try putting that in the function. Not tried yet.

Andy
Hey Andy,

That worked perfectly (changing line 10 from default_random_engine gen(); to default_random_engine gen; to be specific) - although I am definitely still confused... I thought I was using the constructor for the std::default_random_engine class?

Thank you for the help by the way!

EDIT: If I change line 10 from
default_random_engine gen()
to
default_random_engine gen(10)
everything works normally, although being seeded with the same number each time as expected.
Last edited on
although I am definitely still confused... I thought I was using the constructor for the std::default_random_engine class?


It's same as any class - omit the parentheses to call the default constructor:

1
2
3
4
5
6
7
8
MyClass {
   int a;
public:
  MyClass() = default;
}

MyClass MyObject;  // default constructor call
MyClass MyObject(); //Function declaration  


One could start by looking at the examples in the documentation, then go from there:

https://en.cppreference.com/w/cpp/numeric/random
https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

Good Luck !! :+)
Thank you both - My world has been stuck in Excel/VB for the past few years and it's almost embarrassing asking these questions when I look back on my projects from college but you all are awesome and super helpful =)
Topic archived. No new replies allowed.