Jump to content

Math::Random Seed


gamecreator
 Share

Recommended Posts

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;
}

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...