vendredi 24 février 2023

mrand48_r GNU extension

I'm trying to use mrand48_r for a programming assignment, right now, I have something like:

#include <errno.h>
#include <stdlib.h>
#include <assert.h>

// buffer to use                                                                                                                                                            
static struct drand48_data buffer;

static_assert(sizeof(long) == sizeof(unsigned long long));

// initialize mrand48_r                                                                                                                                                     
void
mrand48_init (void)
{
  // zero-filled by default, no init necessary                                                                                                                              
  // memset(&buffer, 0, sizeof(buffer)); same behavior                                                                                                                      

}

// generate values                                                                                                                                                          
unsigned long long
mrand48_wrapper (void)
{
  long result;
  errno = 0;
  if (-1 == mrand48_r(&buffer,&result)){
    perror("mrand48_r");
    abort();
  }
  return (unsigned long long) result;
}

void
mrand48_fini (void)
{
}

on a machine that's compatible with them (portability is not an issue for the assignment).

However, I'm noticing the values generated aren't too random. When generating 20 bytes, about 15 of them don't show up on the screen, and when pointing it to file.txt, I see they are the control character ^@.

What is the proper way to seed this function?




Aucun commentaire:

Enregistrer un commentaire