UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
random.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2023 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
47#ifndef _WIN32
48#include <stdio.h>
49#include <unistd.h>
50#endif
51#define _CRT_RAND_S // request rand_s (Win)
52#include <stdlib.h>
53
54#include "random.h"
55#include "tv.h"
56
63void
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}
80
87uint32_t
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}
99
110double
112{
113 return ug_rand() / (UINT32_MAX + 1.0);
114}
static time_ns_t get_time_in_ns()
Definition tv.h:101
void ug_rand_init(void)
Definition random.c:64
uint32_t ug_rand(void)
Definition random.c:88
double ug_drand()
Definition random.c:111