Page 1 of 1

### Non-repeating random numbers

Posted: Fri Oct 16, 2015 8:25 am
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.

### Re: Non-repeating random numbers

Posted: Fri Oct 16, 2015 4:00 pm
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"?

### Re: Non-repeating random numbers

Posted: Fri Oct 16, 2015 10:03 pm
Hi Ted,

I am considering integers not floating numbers.

Can you kindly let me know how to generate?

### Re: Non-repeating random numbers

Posted: Sat Oct 17, 2015 12:34 pm
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
}``````