UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
random.c File Reference
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "random.h"
#include "tv.h"
Include dependency graph for random.c:

Go to the source code of this file.

Macros

#define _CRT_RAND_S
 

Functions

double ug_drand ()
 
uint32_t ug_rand (void)
 
void ug_rand_init (void)
 

Detailed Description

Author
Martin Pulec marti.nosp@m.n.pu.nosp@m.lec@c.nosp@m.esne.nosp@m.t.cz
Note
Remarks (possible improvements):
  • lbl_random reads from '/dev/urandom' instead if available (so Linux only) and HAVE_DEV_URANDOM defined (which isn't)
  • init_rng() in rtp.c used also hostname for seeding (but there may not be reasonable value, eg. '::1' if no host given to rcvr)

Definition in file random.c.

Macro Definition Documentation

◆ _CRT_RAND_S

#define _CRT_RAND_S

Definition at line 51 of file random.c.

Function Documentation

◆ ug_drand()

double ug_drand ( void )

Behaves similarly to drand48()

uses only 2^32 values from the interval (drand48 may use 48 bits)

Returns
val in interval [0.0, 1.0)
Remarks
thread-safe

Definition at line 111 of file random.c.

112{
113 return ug_rand() / (UINT32_MAX + 1.0);
114}
uint32_t ug_rand(void)
Definition random.c:88

References ug_rand().

Referenced by filter(), and rtcp_interval().

Here is the call graph for this function:

◆ ug_rand()

uint32_t ug_rand ( void )
Returns
pseudorandom number in interval [0, 2^32-1]
Remarks
thread-safe

Definition at line 88 of file random.c.

89{
90#ifdef _WIN32
91 uint32_t ret = 0;
92 rand_s(&ret);
93#else
94 uint32_t ret = (uint32_t) random() << 1;
95 ret |= random() & 0x1;
96#endif
97 return ret;
98}

Referenced by get_std_audio_local_mediatime(), get_std_video_local_mediatime(), init_first(), rtp_init_if(), rtp_init_with_udp_socket(), rtp_send_bye_now(), send_rtcp(), tx_init(), and ug_drand().

◆ ug_rand_init()

void ug_rand_init ( void )
Note
Windows rand_s() is not supposed to be seeded
Remarks
thread-safe

Definition at line 64 of file random.c.

65{
66#ifndef _WIN32
67 unsigned seed =
68 (getpid() * 42) ^ (unsigned) get_time_in_ns() ^ (unsigned) clock();
69 FILE *dev_r = fopen("/dev/random", "rb");
70 if (dev_r != NULL) {
71 unsigned seed_dev_r = 0;
72 if (fread(&seed_dev_r, sizeof seed_dev_r, 1, dev_r) == 1) {
73 seed ^= seed_dev_r;
74 }
75 fclose(dev_r);
76 }
77 srandom(seed);
78#endif
79}
static time_ns_t get_time_in_ns()
Definition tv.h:101

References get_time_in_ns().

Referenced by common_preinit().

Here is the call graph for this function: