/* Copyright (c) 2014 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ // Based on a RANROT-B implementation by Alex Eddy (http://homepage.mac.com/arekkusu/). Thanks Alex! #include "utilities/Ranrot.h" #include #include #include #define stemobject_implementation Ranrot stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(sdrand); stemobject_vtable_entry(stirrand); stemobject_vtable_entry(uirand); stemobject_vtable_entry(irand); stemobject_vtable_entry(ufrand); stemobject_vtable_entry(frand); stemobject_vtable_end(); static bool inited; static Ranrot sharedInstance; Ranrot * Ranrot_create(void) { stemobject_create_implementation(init) } bool Ranrot_init(Ranrot * self) { call_super(init, self); self->protected_ivar(low) = 0; self->protected_ivar(high) = ~0; return true; } void Ranrot_dispose(Ranrot * self) { call_super(dispose, self); } void Ranrot_sdrand(Ranrot * self, int seed) { self->protected_ivar(low) = seed; self->protected_ivar(high) = ~seed; } static void advance(Ranrot * self) { self->protected_ivar(high) = (self->protected_ivar(high) << 16) + (self->protected_ivar(high) >> 16); self->protected_ivar(high) += self->protected_ivar(low); self->protected_ivar(low) += self->protected_ivar(high); } void Ranrot_stirrand(Ranrot * self, int iterations) { while (iterations > 0) { advance(self); iterations--; } } unsigned int Ranrot_uirand(Ranrot * self) { advance(self); return self->protected_ivar(high) & INT_MAX; } int Ranrot_irand(Ranrot * self) { advance(self); return self->protected_ivar(high); } float Ranrot_ufrand(Ranrot * self, float range) { return range * (double) call_virtual(uirand, self) / INT_MAX; } float Ranrot_frand(Ranrot * self, float range) { return range * (double) call_virtual(irand, self) / INT_MAX; } #define readyStaticInstance() \ if (!inited) { \ stemobject_assign_vtable(sharedInstance, Ranrot); \ Ranrot_init(&sharedInstance); \ inited = true; \ } void sdrand(int seed) { readyStaticInstance(); call_virtual(sdrand, &sharedInstance, seed); } void stirrand(int iterations) { readyStaticInstance(); call_virtual(stirrand, &sharedInstance, iterations); } unsigned int uirand() { readyStaticInstance(); return call_virtual(uirand, &sharedInstance); } int irand() { readyStaticInstance(); return call_virtual(irand, &sharedInstance); } float ufrand(float range) { readyStaticInstance(); return call_virtual(ufrand, &sharedInstance, range); } float frand(float range) { readyStaticInstance(); return call_virtual(frand, &sharedInstance, range); }