# Factor that creates a random number within a specified interval with a seed

In Python you can set a range for random number. A specific example would be:

import random
random.uniform(-.001,.001)

Better would be to make a random seed available for example:

import random
random.seed(0)
random.uniform(-.001,.001)

Uses:

1. For rank performance adding a small random number to a factor would make each bucket have the same number of stocks. Useful for the problem posted here: 'Clustered' ranking output - #7 by Jrinne

2. With the seed it could be used to select a sub-universe of stocks. Many people do this using mod() now. The problem with mod is that sometimes the sub-universe is very small and can be used only a limited number of times before exhaustion creative uses of mod(). See this link for a discussion of this problem: How to choose a ranking system? A or B - #6 by test_user

How would one create a random integer between MIN and MAX?

This almost works if I could lop off the decimal portion, like python’s int() does.

@rand:(Random * (MAX - MIN) + 1)

for example where MAX = 5 and MIN = 1 would give me a random number between 1 and 5

But even this isn’t really right because it would only very rarely give me 5, only when Random() returned a 1.

Thanks
Tony

Edit:
I just found this thread from Marco that I think does what I am trying to do.

But I am still curious if a random int can be generated.

Trunc(Random*(Max-Min+1)+Min)

Perfect.
Thanks Yuval.

This is what I was trying to come up with in order to do some kind of monte carlo cross-validation. Marco’s idea is far simpler.

setvar(@Max,5)
setvar(@Min, 1)
@TrueRand:(Trunc(Random*(@Max-@Min+1)+@Min))
mod(StockID, 5) = @TrueRand