Random.value performance

I’m running into a situation where I need lots of random numbers. I know generating a random value can be expensive, and I am contemplating setting up an array of random values at start up to ease the performance hit.

I was thinking that maybe Random.value might do this already, or have some other tricks implemented to keep things fast. Does anyone know?

I don’t think Random.value is expensive and putting values in an array will only add unnecessary overhead.

Take note that random values generated by the computer are pseudo-random. It really spews out a pattern of numbers based on a formula that seems random.

But the pseudo-random number generator’s initial “seed” is set to something that always changes (usually date/time) so that you get different numbers each time the program is run.

I haven’t seen any function that can retrieve the current date, does it exist ?

System.DateTime.Now

Thanx you !

My suggestion is to put the optimization at the end. First do your programming in a high level, simple, easy to read and debug form. Then once it’s all working, benchmark different aspects of your program to find the bottlenecks and then optimize those specific areas. Like anomalous_underdog said, random values aren’t too expensive; I just did a test in Unity and churned out 1,000,000 randoms in 0.032 seconds.

Here’s a good quick writeup on this:
http://www.cs.cmu.edu/~jch/java/rules.html

But it’s missing my first two favourite rules:

Rule 1: Don’t optimize
Rule 2: Don’t optimize yet.

I suggest creating 1 array of a bunch of random numbers, say several thousand, and then cycle through it when you need one. I think it’ll be faster than generating one because creating a random number is not a short operation usually.

I think it’s going to depend on the context. If you have an array of a thousand or so random numbers, they will start repeating themselves as you cycle through the array. If that’s OK, then you can do it that way. If they need to always be random and non-repeating, but you only need a few hundred or thousand each frame, then you’re probably OK generating them on the fly. Maybe you can give us some context as to how many you need to generate each frame and what they’re used for?

The case that made me start thinking about this was flickering on l rocket exhaust flames, of which there may be dozens on screen at any given time, calling Random.value in the update loop. This is an iPhone game.

I really just wanted to know if anybody knew what method Random.value is using. If it’s a simple formula, it’s going to be cheaper to just use that than risk a page fault indexing into an array on the heap.

In that case you may want to use a fixed animation (either hand made or by using a pre-generated random set of numbers) that is made to look random/realistic.

If you want, you could run a script to generate a set of random numbers and use that as a hard-coded, constant array directly in your source code:
(pseudo-code)

private static float[] ExhaustRandomSet = new float[1000]{
	0.2923480f,
	0.9339043f,
	0.2165489f,
	....and so on

I don’t know whether or not you’ll have memory issues from that (I haven’t developed for iPhone)

Best just to benchmark it, then you know.

–Eric