Non-repeating random numbers

Moderator: wwlytton

Post Reply
Krishna Chaitanya
Posts: 70
Joined: Wed Jan 18, 2012 12:25 am
Location: University of Pavia

Non-repeating random numbers

Post by Krishna Chaitanya »

Hi,

Is there a facility in NEURON to generate non-repeating random numbers? I am using random syntax but I don't want to repeat the numbers which already got generated.

Kindly let me know.

Thank you.
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Non-repeating random numbers

Post by ted »

The best answer to your question depends on what you are trying to achieve. Are you generating integers or floating point numbers? If the latter, what is your threshold for deciding that two numbers are "different"?
Krishna Chaitanya
Posts: 70
Joined: Wed Jan 18, 2012 12:25 am
Location: University of Pavia

Re: Non-repeating random numbers

Post by Krishna Chaitanya »

Hi Ted,

I am considering integers not floating numbers.

Can you kindly let me know how to generate?
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Non-repeating random numbers

Post by ted »

Trivial algorithm. In pseudocode

Code: Select all

WHILE number of samples to pick is > 0 {
  REPEAT
    pick i from discrete distribution
  UNTIL i is one that we have not seen before
  append i to the Vector that holds all the samples we have decided to keep
  reduce number of samples to pick by 1
}
"How do I know if I have seen a particular integer before?"

You could use the Vector class's indwhere() method. That might slow down as the number of elements in the Vector grows.

Alternatively, if you know in advance how many samples you need to generate, create a "picked" Vector with that many elements and set all of them to 0. Each element should correspond to one of the integers in the range from which you are sampling. Then

Code: Select all

WHILE number of samples to pick is > 0 {
  REPEAT
    pick i from discrete distribution
  UNTIL you get one for which the corresponding element of the "picked" vector is 0
  return i
  decrease number of samples to pick by 1
}
Post Reply