I would do it this way:
(EDIT: I changed the last two conditions as they were actually redundant.)
(EDIT2: I updated so that the 10/100 offset change happen randomly - but set once at the definition of the function.)
import random def rand_gen_t( t, n0=random.randint(1, 100), n1=random.randint(1, 10)): if t % n0 == 0: offset = 15 elif t % n1 == 0: offset = 5 elif t % 1 == 0: offset = 1 elif t % 0.5 == 0: offset = 2 return offset + random.random()
Compared to the solution proposed by @DillonDavis it would work for arbitrary t as long as it is a half-integer value, and it is assuming that the range you are interested in is always of size 1, and all is changing is the offset (which is true based on the content of your question).
I'll leave to you to define what should be done for other input values.
And if you want this to be able to cope with numpy.ndarray as your tagging suggests, I would just use the np.vectorize function decorator, e.g.:
import numpy as np rand_gen_t_arr = np.vectorize(rand_gen_t)
Time-wise, the proposed Numpy's solution would not really shine here, but is not that bad either:
%timeit [rand_gen_t(x / 2) for x in range(1000)] # 1000 loops, best of 3: 490 µs per loop %timeit rand_gen_t_arr(np.arange(1000) / 2) # 1000 loops, best of 3: 523 µs per loop
Perhaps using np.where() is faster, but I would not expect that, since you would probably have (hidden) a loop for each condition in that way.
EDIT (based on the comments)
If you want this to be be more flexible, you could try something like (assuming you have a predefined array t_arr, containing the value of T):
import numpy as np # I assume that you have it somehow, but I generate one for completeness t_arr = np.arange(1, 1000) / 2 # first generate random numbers between 0 and 1 val_arr = np.random.random(t_arr.shape) # update for values of `T` int_mask = np.where(t_arr % 1 == 0)[0] half_int_mask = np.where(t_arr % 0.5 == 0)[0] int_offset = 1 half_int_offset = 2 val_arr[int_mask] += int_offset val_arr[half_int_mask] += half_int_offset # update `Value` for exceptional cases def gen_special_mask(n_special, n_max): return np.random.randint(1, n_special, int(n_max / n_special)) + np.arange(0, n_max, n_special) def mask_intersect(mask1, mask2): return np.array(list(set(mask1).intersection(set(mask2)))) special_mask10 = gen_special_mask(10, val_arr.size) special10_offset = 5 special_mask100 = gen_special_mask(100, val_arr.size) special100_offset = 10 special_mask10_int = mask_intersect(int_mask, special_mask10) val_arr[special_mask10_int] += (special10_offset - int_offset) special_mask10_half_int = mask_intersect(half_int_mask, special_mask10) val_arr[special_mask10_half_int] += (special10_offset - half_int_offset) special_mask100_int = mask_intersect(int_mask, special_mask10) val_arr[special_mask100_int] += (special100_offset - int_offset) special_mask100_half_int = mask_intersect(half_int_mask, special_mask10) val_arr[special_mask100_half_int] += (special100_offset - half_int_offset)