c - Random number generator code fix (alternative for srand48/drand48) -


let me start saying know near nothing c , c++. in fact, mathematics major, i'm totally new programming. being said, need wrap older code trying struggle through it. now, when trying run test, error

severity code description project file line suppression state error lnk2019 unresolved external symbol srand48 referenced in function main pokereval c:\users\m.faas\documents\visual studio 2017\projects\pokerproject\pokerevalc\pokereval\allfive.obj 1
error lnk2019 unresolved external symbol drand48 referenced in function shuffle_deck pokereval c:\users\m.faas\documents\visual studio 2017\projects\pokerproject\pokerevalc\pokereval\pokerlib.obj 1
error lnk1120 2 unresolved externals pokereval c:\users\m.faas\documents\visual studio 2017\projects\pokerproject\pokerevalc\pokereval\x64\debug\pokereval.exe 1

and same drand48.

the code i'm using starts follows:

#include <stdio.h> #include "arrays.h" #include "poker.h"  void srand48(); double drand48(); 

and continues use srand48 , drand48 in these snippets of code:

// seed random number generator. srand48(getpid()); 

and drand48:

// //  routine takes deck , randomly mixes //  order of cards. // void shuffle_deck(int *deck) {     int i, n, temp[52];      (i = 0; < 52; i++)         temp[i] = deck[i];      (i = 0; < 52; i++)     {         {             n = (int)(51.9999999 * drand48());         } while (temp[n] == 0);         deck[i] = temp[n];         temp[n] = 0;     } } 

the full code can found here: http://suffe.cool/poker/code/

but can't think of way fix issue, though seems minor. said, speed absolutely essential, code run several millions of times. i'm using visualstudio on x64 machine.

these declarations

void srand48(); double drand48(); 

are deprecated, should use full prototypes instead:

void srand48(long); double drand48(void); 

or better include header declared.

but problem these functions don't exist on windows. remove these declarations. can emulate these functions (with maybe bit lower quality of random numbers) using standard srand() , rand() functions (declared in stdlib.h) this:

#define srand48(x) srand((int)(x)) #define drand48() ((double)rand()/rand_max) 

here's little poc example:

#include <stdlib.h> #include <stdio.h> #include <time.h>  #define srand48(x) srand((int)(x)) #define drand48() ((double)rand()/rand_max)  int main(void) {     srand48(time(0));      (;;)     {         printf("%f\n", drand48());     } } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -