gamecreator Posted September 23, 2013 Share Posted September 23, 2013 Math::Random() always returns the same value. Does anyone know a way to seed it? I've seen it done via Lua but this should be possible via C. Below is sample code which is just the example code with a few extra lines (just float rnum as global, rnum=Math::Random() in Start and a few lines in Loop to print it). #include "App.h" using namespace Leadwerks; App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {} App::~App() { delete world; delete window; } Vec3 camerarotation; #if defined (PLATFORM_WINDOWS) || defined (PLATFORM_MACOS) bool freelookmode=true; #else bool freelookmode=false; #endif float rnum; bool App::Start() { //Create a window window = Window::Create("RandomTest"); //Create a context context = Context::Create(window); //Create a world world = World::Create(); //Create a camera camera = Camera::Create(); camera->Move(0,2,-5); //Hide the mouse cursor window->HideMouse(); std::string mapname = System::GetProperty("map","Maps/start.map"); Map::Load(mapname); //Move the mouse to the center of the screen window->SetMousePosition(context->GetWidth()/2,context->GetHeight()/2); rnum=Math::Random(); return true; } bool App::Loop() { //Close the window to end the program if (window->Closed()) return false; //Press escape to end freelook mode if (window->KeyHit(Key::Escape)) { if (!freelookmode) return false; freelookmode=false; window->ShowMouse(); } if (freelookmode) { //Keyboard movement float strafe = (window->KeyDown(Key:) - window->KeyDown(Key::A))*Time::GetSpeed() * 0.05; float move = (window->KeyDown(Key::W) - window->KeyDown(Key::S))*Time::GetSpeed() * 0.05; camera->Move(strafe,0,move); //Get the mouse movement float sx = context->GetWidth()/2; float sy = context->GetHeight()/2; Vec3 mouseposition = window->GetMousePosition(); float dx = mouseposition.x - sx; float dy = mouseposition.y - sy; //Adjust and set the camera rotation camerarotation.x += dy / 10.0; camerarotation.y += dx / 10.0; camera->SetRotation(camerarotation); //Move the mouse to the center of the screen window->SetMousePosition(sx,sy); } Time::Update(); world->Update(); world->Render(); context->SetColor(0,0,0); context->Clear(); context->SetBlendMode(Blend::Alpha); context->SetColor(1,1,1); context->DrawText("Random number: " + String(rnum),10,10); context->Sync(false); return true; } Quote Link to comment Share on other sites More sharing options...
kennar Posted September 23, 2013 Share Posted September 23, 2013 If I'm not mistaken you'll have to seed the random number generator with something like the following. unsigned int randomize (void) { time_t timeval = time (NULL); unsigned char *ptr = (unsigned char *) &timeval; unsigned seed = 0; size_t i; for (i = 0; i < sizeof timeval; i++) seed = seed * (UCHAR_MAX + 2U) + ptr[i]; srand (seed); return seed; } Quote Link to comment Share on other sites More sharing options...
gamecreator Posted September 23, 2013 Author Share Posted September 23, 2013 Thank you kennar. I'm familiar with the method you show but I'm looking for an official Leadwerks function that is compatible with at least Android. I've had issues trying non-Leadwerks functions before and them not working (here). Quote Link to comment Share on other sites More sharing options...
kennar Posted September 23, 2013 Share Posted September 23, 2013 Understood, I too went looking for an LE3 solution to the same this past week. Quote Link to comment Share on other sites More sharing options...
gamecreator Posted September 23, 2013 Author Share Posted September 23, 2013 Until Josh or someone else presents a solution, I think a weird workaround will be to do something like this: for(int i=0; i<((int)Time::Millisecs())%10000; i++) Math::Random(); That will run Random a "random" amount of times, if my code is right (would probably need to check for negative numbers and probably use Math::Mod instead of %). That or I may have to try and brave learning Lua but I'd hate to spend the time and have it be an additional factor of things potentially breaking, just to seed Random. Josh? Quote Link to comment Share on other sites More sharing options...
Rick Posted September 24, 2013 Share Posted September 24, 2013 If I remember correctly the same LE function in Lua had the same issue. For lua I just use the built-in Lua random functions. I have to imagine srand() works on Android and even iOS though? Quote Link to comment Share on other sites More sharing options...
Admin Posted September 24, 2013 Share Posted September 24, 2013 The C++ function srand will set the seed: http://www.cplusplus.com/reference/cstdlib/srand/ If I remember right I was looking at this a while ago, and there is no cross-platform way to retrieve the current seed value. Quote Link to comment Share on other sites More sharing options...
gamecreator Posted September 24, 2013 Author Share Posted September 24, 2013 I just tested this and was about to post when you did. Adding srand((int)Time::Millisecs()); just before rnum=Math::Random(); will indeed seed it. Very nice to see this. I also stand corrected on my other thread. I tested if(window->TouchDown(0)) rnum=sqrt(4.0); on Android and now it worked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.