Recording data and generating distinct integers

Anything that doesn't fit elsewhere.
Post Reply
Skyfire79

Recording data and generating distinct integers

Post by Skyfire79 »

Hi, Ted,

I got two questions for you. The first one is about recording simulation time. I used a time step of dt = 0.05 ms (fixed) in simulation and I want to record the simulation time in every 10 steps. My code is as follows:

Code: Select all

N  = 10
objref f1
f1 = new File()
objref time
time = new Vector()
// Record time
time.record(&t, N*dt)
// Save time into a file
f1.wopen("/Model/NET/data/tt")
time.printf(f1)
f1.close()


If everything is corret, the data in the file "tt" should be: 0 0.5 1 1.5 2... However, the actual data I got is somewhat different:
0
0.475
1
1.5
2
2.475
2.975
3.475
3.975
.
.
So sometimes it recorded correctly, but sometimes it was off a little bit (e.g., 0.475 instead of 0.5). That means the data was not recorded in an uniform interval. Why?

The second question is how I can generate a number of distinct integers in NEURON? I used the code to generate N integers:

Code: Select all

objref random
random = new Random(seed)
for k=0, N {
    gi = random.uniform(0, 100)
    gi = nint(gi)
}

func nint() {
  if ( abs($1 - int($1)) <= 0.5) {
    return int($1)
  } else {
    if ($1 < 0) { return int($1)-1 }
    if ($1 >= 0) { return int($1)+1 }
  }
}
But this code would generate repeated integers. Is there an efficient way to generate distinct integers?

Thank you very much!

Best,
GL
ted
Site Admin
Posts: 6398
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Recording data and generating distinct integers

Post by ted »

Skyfire79 wrote:If everything is corret, the data in the file "tt" should be: 0 0.5 1 1.5 2... However, the actual data I got is somewhat different:
0
0.475
. . .
3.975
.
So sometimes it recorded correctly, but sometimes it was off a little bit (e.g., 0.475 instead of 0.5). That means the data was not recorded in an uniform interval. Why?
Roundoff error. Numbers with fractional parts may have representations in base 2 that are truncated because of the finite precision of the floating point library.
how I can generate a number of distinct integers in NEURON?
This is called "sampling without replacement." One way to achieve the goal is to create a Vector with N elements whose values are 0. Each time your pseudorandom number generator returns a value, check to see if the corresponding element in your Vector is 0. If it is, set it to 1 and keep the number. If it isn't, discard the number and pick a new one.

There is an extensive literature on algorithms for all kinds of purposes, and several books--some in paperback--have been written on the topic that introduce some of the most useful ones. You might browse the nearest academic library or even check out Amazon.
Skyfire79

Re: Recording data and generating distinct integers

Post by Skyfire79 »

Hi, Ted,

Thank you very much for answering my questions! If I understand it correctly, there is nothing I can do with the roundoff error, right? When I plot the data (e.g., voltage), should I use the simulation time I recorded (i.e., 0 0.475 1...), or I should generate a time vector with 0.5 interval (i.e., 0 0.5 1 1.5...)? I know they are just a little bit different, but want to know which one is more accurate.

About your approach to generate N distinct integers, I still don't get it at this time. If the initial values of the vector with N elements are 0, then each time the pseudorandom number generator returns a value, the corresponding element in the vector is always zero,right? For example, the first time the generator returns a value, the 1st element in the vector is 0, and the 10th time the generator returns a value, the 10th element in the vector is still 0 because nothing has changed the initial values of the 10th element and the elements after it. Anyway, I will try to figure it out later.

Thanks again and happy holiday!

Best,
GL
ted
Site Admin
Posts: 6398
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Recording data and generating distinct integers

Post by ted »

Suppose you start out with two vectors: one called samples which has length 0, and another called taken which has N elements whose values are all 0.

Code: Select all

Repeat
  "pick" a new integer i
  if the ith element of taken is 0
    append i to samples
    change the ith element of taken to 1
Until samples has N elements
Skyfire79

Re: Recording data and generating distinct integers

Post by Skyfire79 »

Ted,

I got it now. It is a very efficient way to solve my problem. Thank you so much for your help!

Best,
GL
Post Reply