views:

23

answers:

1

I work with a lot of random numbers to create constantly shifting textures. I'm getting tired of doing things like:

 ((rand() % 1000) / 10) + 100 

when I really want something like

[randomUtils randomNumberBetween:100 and:200]

Additonally, I'd like to be able to manipulate weights of the randomization, so that, for example, my values would tend to fall closer to 100, and only rarely approach 200.

Does something like this exist already, or should I just write it myself?

+1  A: 

This is, IMO, a nice use for a class with mostly (if not all) class methods. For example, you could create a class called RURandomUtilities. Within the class you could do:

+ (NSInteger) randomNumberBetween:(NSInteger) start and:(NSInteger) end
{
    return ((rand() % (start - end) +start);
}

and so on. Depending on if you need lots of instances going of different random numbers, you could make these classes that are instantiated with basic parameters for weighting and seeds.

If you need consistent performance of all random numbers across your applications, local static singletons could be used to hold the weighting. The only instantiated object would be the class itself.

Steven Noyes
Thanks Steven. That does indeed sound like a nice way to structure it. I'm just wondering if there's a nice, open source every-type-of-randomization-and-scaling package already available somewhere.
morgancodes
I have not seen one but it could be written in about 30 minutes I would think if you just need some basic features. A couple hours if you want a bit more sophistication.
Steven Noyes