VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
UbRandom.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef UBRANDOM_H
35#define UBRANDOM_H
36
37#include <cassert>
38#include <cmath>
39#include <cstdlib>
40#include <ctime>
41
42/*=========================================================================*/
43/* UbRandom */
44/* */
51/*
52usage:
53 int main()
54 {
55 char* hand[] = {"Schere", "Stein", "Papier"};
56 for (unsigned u = 0; u < 20; u++)
57 {
58 cout << hand[UbRandom::rand(0, 2, 1)] << endl;
59 }
60
61 return 0;
62 }
63*/
64
66{
67private:
68 UbRandom() { std::srand(static_cast<int>(std::time(NULL))); }
69
70public:
71 // returns arbitrary int value element of [min ; max]
72 static inline int rand(const int &min, const int &max)
73 {
74 static UbRandom dummy;
75 assert(max - min < RAND_MAX);
76 return (min + std::rand() % (max - min + 1));
77 }
78 // returns arbitrary float value element of "( (max - min) / gran ) * [min ; max]"
79 // with other words: val = min+n*(max-min)/gran, n=0..gran-1
80 static inline double rand(const double &min, const double &max, const double &gran)
81 {
82 static UbRandom dummy;
83 return (min + std::floor(std::rand() / (1.0 + RAND_MAX) * gran) * (max - min) / gran);
84 }
85};
86
87#endif // UBRANDOM_H
88
static int rand(const int &min, const int &max)
Definition UbRandom.h:72
static double rand(const double &min, const double &max, const double &gran)
Definition UbRandom.h:80
std::shared_ptr< T > SPtr