#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)
Change History (22)
comment:1 Changed 17 years ago by
comment:2 Changed 17 years ago by
Owner: | changed from Alpar Juttner to Balazs Dezso |
---|---|
Type: | defect → enhancement |
Version: | svn trunk → hg main |
Changed 17 years ago by
Attachment: | random_seeding.bundle added |
---|
comment:3 Changed 17 years ago by
Status: | new → assigned |
---|
The uploaded boundle contains seeder functions.
comment:4 Changed 17 years ago by
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
It is now in the main branch: [81563e019fa4].
comment:5 Changed 17 years ago by
Resolution: | fixed |
---|---|
Status: | closed → reopened |
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 17 years ago by
Milestone: | → Post 1.0 |
---|
Changed 17 years ago by
Attachment: | randomize.bundle added |
---|
comment:7 follow-up: 8 Changed 17 years ago by
I have uploaded a bundle with randomize function, and a doc improvement.
comment:8 follow-up: 9 Changed 17 years ago by
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 follow-up: 10 Changed 17 years ago by
Replying to alpar:
Replying to deba:
I have uploaded a bundle with randomize function, and a doc improvement.
Couldn't we use
seed()
for thisrandomize()
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 Changed 17 years ago by
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 follow-up: 12 Changed 17 years ago by
The long long int is not a ansi or iso:iec standard type.
comment:12 Changed 17 years ago by
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 follow-up: 14 Changed 17 years ago by
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 Changed 17 years ago by
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 17 years ago by
Attachment: | b685e12e08c0.patch added |
---|
comment:15 follow-ups: 16 18 Changed 17 years ago by
In the [b685e12e08c0] patch the randomize() function is renamed to seed().
comment:16 follow-up: 17 Changed 17 years ago by
Replying to deba:
In the [b685e12e08c0] patch the randomize() function is renamed to seed().
This patch replaces randomize.bundle, doesn't it?
comment:17 Changed 17 years ago by
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 Changed 17 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
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 16 years ago by
Milestone: | Post 1.0 → LEMON 1.0 release |
---|
Okay, you can get over this limitation by this:
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.