Recursive Randomness

Blitz3D Forums/Blitz3D Programming/Recursive Randomness

ClayPigeon(Posted 2011) [#1]
I have a question. (obviously or why else would I be here?) If some choices are invalid, is it acceptable to keep asking for random numbers until a valid one is obtained? For example, if I execute Rand(0,15), and all numbers except 3 & 12 are invalid, is it acceptable to repeat the Rand(0,15) function until I hit a valid number? I don't know what the probability of the program hitting invalid numbers is and I'm worried that there is a chance that the program could get hung up from this.


GfK(Posted 2011) [#2]
Its not something I'd do repeatedly on-the-fly (like, every frame), but as an occasional thing or one-off you shouldn't have any problems. I don't have BB3D installed but I just ran some tests on my laptop in Blitzmax and can get through 100,000 Rand() iterations in release mode, in under 10ms.

That said, I'd always look for a better solution first but having to do it is not the end of the world.


Yasha(Posted 2011) [#3]
(Edit: original suggestion was really stupid because I misunderstood)

Depending upon what kind of "randomness" you need, the simplest thing to do might be to simply increment/loop round to zero until you hit an open slot in the list of numbers? It wouldn't get you an open slot in a constant amount of time, but it would guarantee that you get one eventually as long as not all the numbers are invalid.

Of course if you didn't have a check to see whether all the numbers were invalid, or if you'd been around the entire list, then you'd have a guaranteed infinite loop.

(original suggestion) For short lists of numbers such as the example (say under 100 numbers) you could also keep a list or array of valid results and retrieve the actual number by selecting a random index from the array of valid numbers, rather than the number itself. Obviously this becomes impractical for very large ranges.

Last edited 2011


H&K(Posted 2011) [#4]
I think Yasha is right, if I want to get a rnd number and only two numbers are valid, I would definitely just rnd for two outcomes (Rnd (0,1)) and then sort out what they represent.

Edit, to be clear I think he was right in the post before he edited. I dont like his new answer, cos I assume you want the same chance for any of the remaining numbers

Last edited 2011


Subirenihil(Posted 2011) [#5]
The way I'd recommend is to have an array of all possible valid answers and then Rand(0, <number of possible answers> - 1).

Dim answers(10)
For a=0 to 9
 answers(0)=a+1
Next
possibles=4
Print answers(Rand(0, possibles-1))

By having an array, you can dynamically alter your choices.
;To add an answer
new_number=Rand(10)
answers(possibles)=new_number
possibles=possibles+1

;To subtract an answer
invalid_answer=Rand(0, possibles-1)
answers(invalid_answer)=answers(possibles-1)
possibles=possibles-1

Guaranteed to only give you valid choices.


_PJ_(Posted 2011) [#6]
Certainly for a lot of cases, iterations of Rand() wont make too much difference. As Gfk stated, the CPU can process the check millions of times a second.
If the situation is particularly time intensive, and/or the ratio of 'valid' results is very small, and/or the number of possibilities is largte, then one of the other suggestions presented may be better practice.

I like Subirenhil's suggestion, though whether or not it's really practical is another matter and again, relies heavily on the specific circumstance.

Ultimately, consider the actual end result of what you need to achieve. The rule of thumb I like to work by is to, in the first instance, use a simple system that works. Only later address the parts that require optimisation.