COIN-OR::LEMON - Graph Library

Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#19 closed enhancement (fixed)

Global rnd object is not seedable/randomizable

Reported by: Mihaly Barasz Owned by: Balazs Dezso
Priority: minor Milestone: LEMON 1.0 release
Component: core Version: hg main
Keywords: Cc:
Revision id:

Description

random.h provides a global instance of Random, which is indeed convenient, but pretty much useless now. As there is no way to provide a seed for it. So you'll get the same sequence every time you run your program.

I suggest to create a seed(...) method(s) to the Random class, which would reseed the sequence.

Please, also consider the possibility to implement some kind of randomize() method, to seed the sequence by pid of the process, timeofday etc. Sure, this is not cryptographically secure, but very convenient for doing simulations...

Attachments (3)

random_seeding.bundle (1.1 KB) - added by Balazs Dezso 11 years ago.
randomize.bundle (1.7 KB) - added by Balazs Dezso 11 years ago.
b685e12e08c0.patch (3.5 KB) - added by Balazs Dezso 10 years ago.

Download all attachments as: .zip

Change History (22)

comment:1 Changed 11 years ago by Mihaly Barasz

Okay, you can get over this limitation by this:

lemon::rnd = lemon::Random(seed);

But this is very non-intuitive in my opinion. So, I still think that seed(...) and randomize() methods are needed. Or at least there should be some demo for this usage in docs.

comment:2 Changed 11 years ago by Alpar Juttner

Owner: changed from Alpar Juttner to Balazs Dezso
Type: defectenhancement
Version: svn trunkhg main

Changed 11 years ago by Balazs Dezso

Attachment: random_seeding.bundle added

comment:3 Changed 11 years ago by Balazs Dezso

Status: newassigned

The uploaded boundle contains seeder functions.

comment:4 Changed 11 years ago by Alpar Juttner

Resolution: fixed
Status: assignedclosed

It is now in the main branch: [81563e019fa4].

comment:5 Changed 11 years ago by Alpar Juttner

Resolution: fixed
Status: closedreopened

I reopen the ticket as the randomize() feature is still unimplemented. This feature would be useful, though it might not be fully straightforward to do. Here is a related comment of deba from ticket #42:

The randomize feature is a good idea and I would welcome it in the LEMON. However the solution is not obvious. In the c++ standard I have found just two functions which could be the source of the randomness(the clock() and the time() functions), but both of them have strict limitations. The good randomize function should work whenever we use more threads with own generators approximately started in the same time. Therefore, I suggest that it should be implemented different way for each compiler(for example MSVC does not supports getpid() function just _getpid()).

The other thing is that I would use the seed() member function without any parameter for this purpose.

comment:6 Changed 11 years ago by Alpar Juttner

Milestone: Post 1.0

Changed 11 years ago by Balazs Dezso

Attachment: randomize.bundle added

comment:7 Changed 11 years ago by Balazs Dezso

I have uploaded a bundle with randomize function, and a doc improvement.

comment:8 in reply to:  7 ; Changed 11 years ago by Alpar Juttner

Replying to deba:

I have uploaded a bundle with randomize function, and a doc improvement.

Couldn't we use seed() for this randomize() function, as well?

comment:9 in reply to:  8 ; Changed 11 years ago by Balazs Dezso

Replying to alpar:

Replying to deba:

I have uploaded a bundle with randomize function, and a doc improvement.

Couldn't we use seed() for this randomize() function, as well?

The seed() function is a template function with one or two parameters, while the current randomize function needs a parameter which is the file name of random source (the default is "/dev/random"). The overriding of a template function is not a good possibility in c++, because it excludes the execution of each implicit conversions.

comment:10 in reply to:  9 Changed 11 years ago by Alpar Juttner

The seed() function is a template function with one or two parameters, while the current randomize function needs a parameter which is the file name of random source (the default is "/dev/random"). The overriding of a template function is not a good possibility in c++, because it excludes the execution of each implicit conversions.

So the reason for this whole problem is that seed(Number) is a template function. Why is it a template function? Why don't we use just seed(long long int) instead?

comment:11 Changed 11 years ago by Balazs Dezso

The long long int is not a ansi or iso:iec standard type.

comment:12 in reply to:  11 Changed 11 years ago by Alpar Juttner

Replying to deba:

The long long int is not a ansi or iso:iec standard type.

I don't care too much about it.

  • The vast majority of the computers sold today is a 64 bit architecture.
  • All of the compilers with which we have ever tested to compile LEMON support long long int.

Thus I must say, this is the problem of ANSI, not ours.

But if we want to be foolproof, we could restrict to seed(unsigned int) in case when long long int is not available (by an appropriate #ifdef).

comment:13 Changed 11 years ago by Balazs Dezso

In my opinion, the randomize is a good name for this feature. I do not feel any achievement of using overloading in this case.

comment:14 in reply to:  13 Changed 11 years ago by Alpar Juttner

Replying to deba:

In my opinion, the randomize is a good name for this feature. I do not feel any achievement of using overloading in this case.

Well, I don't think it's a good name. The function randomize() does exactly the same as the functions we currently call seed(): they all set the random seed based on some data source. So it is reasonable to use the same name for them.

So, it there any problem with the solution I proposed above?

Changed 10 years ago by Balazs Dezso

Attachment: b685e12e08c0.patch added

comment:15 Changed 10 years ago by Balazs Dezso

In the [b685e12e08c0] patch the randomize() function is renamed to seed().

comment:16 in reply to:  15 ; Changed 10 years ago by Alpar Juttner

Replying to deba:

In the [b685e12e08c0] patch the randomize() function is renamed to seed().

This patch replaces randomize.bundle, doesn't it?

comment:17 in reply to:  16 Changed 10 years ago by Balazs Dezso

Replying to alpar:

Replying to deba:

In the [b685e12e08c0] patch the randomize() function is renamed to seed().

This patch replaces randomize.bundle, doesn't it?

Yes, it is. It contains several improvements, as the different seeding methods can be called directly and an offset can be set in the seedFromFile() member, and of course the functions are named as seed*().

comment:18 in reply to:  15 Changed 10 years ago by Alpar Juttner

Resolution: fixed
Status: reopenedclosed

Replying to deba:

In the [b685e12e08c0] patch the randomize() function is renamed to seed().

Thanks. It went to the main branch. See also [d2bac07f1742], which contains some doc improvements.

comment:19 Changed 10 years ago by Alpar Juttner

Milestone: Post 1.0LEMON 1.0 release
Note: See TracTickets for help on using tickets.