Initializing seed with the system time

Anything that doesn't fit elsewhere.
Post Reply
miller

Initializing seed with the system time

Post by miller »

Hi!

I would like to initialize the seed of the Random class with the system time. Because I don't know how to do this from hoc code, I wrote a NMODL POINT_PROCESS

Code: Select all

NEURON {
  POINT_PROCESS c_functions
}

VERBATIM
	#include <time.h>
ENDVERBATIM

FUNCTION GetSystemTime(){
	VERBATIM
	return time(NULL);
	ENDVERBATIM
}
I successully compile it in WinXP with mknrndll.hoc. But when I then call GetSystemTime() from the hoc code I get the error message
Undefined symbol _time referenced from nrnmech.dll.
Anyone knows what should I do?
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Groningen, The Netherlands
Contact:

Post by Raj »

Although only half answer it might hint at another solution direction, under XP with cygwin installed alongside it is possible to do the following:

Code: Select all

strdef datetime
objref szFunctionsObject
szFunctionsObject = new StringFunctions()

system("date +%Y%m%d%H%M%S",datetime)
	szFunctionsObject.left(datetime,14)			//The date function might produce a newline (it does with cygwin)
This gives you back the system datetime as a string. With sscanf you should be able to convert that to number. The date man page give several other forms including seconds from a time somewhere in 1970 so you can pick what is most convenient.

You can actually inspect the relevant man pages from within neuron, just type system("man date") from the command line and see what happens.
miller

Post by miller »

Thanks! It works perfectly! That's exactly what I needed!

Konstantin
tsa
Posts: 31
Joined: Mon Mar 26, 2007 12:44 pm

Post by tsa »

I have been trying to use sscanf for this same task to convert the system date/time string to a number for seeding but I've been unsuccessful due to my ignorance of the formats used for sscanf.

Every format I've tried listed in the programmers references returns an error of incorrect argument type. Could anyone give a quick sscanf lesson?
abogaard
Posts: 8
Joined: Mon Feb 05, 2007 4:21 pm
Location: ann arbor, mi

Post by abogaard »

Code: Select all

szFunctionsObject.left(strSeed,14)
is an important step before using sscanf, because I think there's a line break in the string generated by default. Then you can use

Code: Select all

sscanf(strSeed,"%lf",&intSeed)
Technically its a floating point number, but nowhere in your string assignment do you have a decimal point, so you'll get an integer.
tsa
Posts: 31
Joined: Mon Mar 26, 2007 12:44 pm

Post by tsa »

What is the file type of "intSeed". I'm getting errors that it is not a double pointer but if i declare intSeed as a double NEURON gets stuck in a segmentation violation error loop. Thanks,
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Groningen, The Netherlands
Contact:

Post by Raj »

Tsa,

Can you provide us with the shortest possible bit of code that shows your problem. What your wrote sofar gives me the impression that your are overlooking something simple quite unrelated to the details of sscanf.

Regards,
Raj
tsa
Posts: 31
Joined: Mon Mar 26, 2007 12:44 pm

Post by tsa »

You were right Raj, I was putting my variable for strSeed in quotes within sscanf cause the errors. Thanks for your input
Post Reply