473,549 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

seeding function

Ok, I know that srand( time(NULL) ) isn't such a good idea, but I do it
anyway. Why? The clock is an easy source of randomness. And I've got this
sneaky suspicion that I'm not the first guy to commit this foul atrocity
against the finer design principles of RNGs in general...

So here's me thought: Design a seeding function that has only one purpose --
and that purpose is to use time(NULL) as it's input, and generate a
reasonably random output for seeding a random number generator.

As of right now, the value of time(NULL) on my machine is: 1095397643 (1.095
billion), and increments once per second. the return value is of time_t,
which according to www.cplusplus.com is generally defined as a long. It's
four bytes on my machine.

USAGE...here's where things differ markedly from "normal" RNGs:

It occurs to me that this function doesn't really have to return a different
series ten thousand years from now. Most people are going to use it in rapid
succession several times over the course of an hour or three -- usually the
same hour or three in a day. Even more to the point, RAND_MAX is usually
defined as 32768, or 2^15.

Thus what we have here is a function that maps a spread of numbers from one
domain to another, and we are reasonably sure the domain to map to is the
2^15 random numbers in the range [0..32768).

Now another thing to notice: We don't need thirty two thousand random seeds.
Most of the time, only the first number is used, and far less often the
first dozen or hundred.

How then can I approach the problem of selecting just one random number
given a sequential seed? It occurs to me to perhaps use some standard number
sequence, maybe offset or modified?

Jul 22 '05 #1
2 1787
I'm not sure wether you want to build a random number generator or you just
want to use one

If the last is the case then I would use a standard random number generator
package. Such issues about seeds have been dealt with and you do not want to
reinvent it.

you can find a good package on www.netlib.org (ranlib) A newer generator is
mersene twister, look for it on the web

I would be carefull with just simply using rand()

stijn

"AngleWyrm" <no************ ***@hotmail.com > schreef in bericht
news:jBv2d.2069 49$Fg5.114842@a ttbi_s53...
Ok, I know that srand( time(NULL) ) isn't such a good idea, but I do it
anyway. Why? The clock is an easy source of randomness. And I've got this
sneaky suspicion that I'm not the first guy to commit this foul atrocity
against the finer design principles of RNGs in general...

So here's me thought: Design a seeding function that has only one purpose -- and that purpose is to use time(NULL) as it's input, and generate a
reasonably random output for seeding a random number generator.

As of right now, the value of time(NULL) on my machine is: 1095397643 (1.095 billion), and increments once per second. the return value is of time_t,
which according to www.cplusplus.com is generally defined as a long. It's
four bytes on my machine.

USAGE...here's where things differ markedly from "normal" RNGs:

It occurs to me that this function doesn't really have to return a different series ten thousand years from now. Most people are going to use it in rapid succession several times over the course of an hour or three -- usually the same hour or three in a day. Even more to the point, RAND_MAX is usually
defined as 32768, or 2^15.

Thus what we have here is a function that maps a spread of numbers from one domain to another, and we are reasonably sure the domain to map to is the
2^15 random numbers in the range [0..32768).

Now another thing to notice: We don't need thirty two thousand random seeds. Most of the time, only the first number is used, and far less often the
first dozen or hundred.

How then can I approach the problem of selecting just one random number
given a sequential seed? It occurs to me to perhaps use some standard number sequence, maybe offset or modified?

Jul 22 '05 #2
"Stijn Oude Brunink" <so**********@c hello.nl> wrote in message
news:Oj******** **********@amsn ews03.chello.co m...
I'm not sure wether you want to build a random number generator or you just want to use one
Specifically, I am interested in developing a seeding function for
std::rand(), which would serve as a replacement for srand().
If the last is the case then I would use a standard random number generator package. Such issues about seeds have been dealt with and you do not want to reinvent it.


Actually, I do want to reinvent it -- the issues I'm interested in don't
appear to have been dealt with. The mersenne twister uses a 32-bit seed, at
the very least. Seeding the mersenne twister with time(NULL) (~1.09 billion)
every time isn't going to be much of an improvement.

State information for std::rand() can be stored, so that multiple instances
may have different sequences, and the user doesn't use up dimensionality on
several purposes. Here is an improvement on std::rand():

////////////////////////////////////////////////////////////////////////////
////
// marginal improvement on std::rand()
// This functor stores state information,
// so that multiple instances can have different seeds.
class rng_class {
public:
void seed( unsigned int s ){ current = s; };
unsigned int operator()(unsi gned int range)
{
if ( range == 0 ) return 0; // only one possibility.

// save global rand state, and restore this instance's state
unsigned int global_state = rand();
srand( current );

// handle cases where range doesn't evenly divide RAND_MAX,
unsigned int bucket_size = RAND_MAX / range;
unsigned int result;
do {
current = std::rand();
result = current / bucket_size;
} while ( result >= range );

srand( global_state ); // restore global state

return result;
}
protected:
unsigned int current;
};
The problem isn't the generators, it's the use of time(NULL) for seeding.
This is the reason for this thread: The generators are ok.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14914
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
3
7361
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The...
5
2814
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function invisible() { alert("invisible() called"); } }
4
2687
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
2
7665
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this...
2
12668
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
3
2168
by: bobrics | last post by:
Hi, I am using srand() and would like to create different random numbers during a SINGLE execution of my program because I want to compare random cases. For now I have a switch statement within a loop, which does not do the job. I get the same output from my simulator every time. Does it mean that it actually can seed ONCE per program run?...
13
3174
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister, with it's massive period of 2^19937-1. Will you only ever have access to a tiny portion of this ring of numbers, if you only use a 32-bit seed? Will...
2
3626
by: HumanJHawkins | last post by:
I wrote this question a little differently in the MFC group since it is a bit of a different environment than pure C++, but I hope you will forgive the similarities if anyone is reading both groups. Basically, I need to seed the random number generator in 4 seperate threads at once. So, I can't use the current time as the random seed,...
0
7524
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.