I know this is a rather simple question, but I'm just not too good at maths.
I know how to generate a random float between 0 and 1:
float random = ((float) rand()) / (float) RAND_MAX;
But what, if I want a function that given a range of two floats, returns a pseudorandom float in that range?
Example:
RandomFloat( 0.78, 4.5 ); //Could return 2.4124, 0.99, 4.1, etc.
float RandomFloat(float a, float b) {
float random = ((float) rand()) / (float) RAND_MAX;
float diff = b - a;
float r = random * diff;
return a + r;
}
This works by returning a plus something, where something is between 0 and b-a which makes the end result lie in between a and b.
float RandomFloat(float min, float max)
{
// this function assumes max > min, you may want
// more robust error checking for a non-debug build
assert(max > min);
float random = ((float) rand()) / (float) RAND_MAX;
// generate (in your case) a float between 0 and (4.5-.78)
// then add .78, giving you a float between .78 and 4.5
float range = max - min;
return (random*range) + min;
}
Random between 2 float :
float random_between_two_int(float min, float max)
{
return (min + 1) + (((float) rand()) / (float) RAND_MAX) * (max - (min + 1));
}
Random between 2 int :
int random_between_two_int(float min, float max)
{
return rand() % (max - min) + min + 1;
}
Suppose, you have MIN_RAND and MAX_RAND defining the ranges, then you can have the following:
const float MIN_RAND = 2.0, MAX_RAND = 6.0;
const float range = MAX_RAND - MIN_RAND;
float random = range * ((((float) rand()) / (float) RAND_MAX)) + MIN_RAND ;
This will provide you the number scaled to your preferred range.
MIN_RAND, MAX_RAND can be any value, like say 2.5, 6.6
So, the function could be as:
float RandomFloat(float min, float max) {
return (max - min) * ((((float) rand()) / (float) RAND_MAX)) + min ;
}
Related
This is an example of my code:
float a = 0.f;
float b = 5.f;
float increment = 0.1f;
while(a != b)
a+=increment;
This will result in an infinite loop. Is there any solutions to it, or the only way to solve this is to set a tolerance?
Avoid using floating-point calculation when possible. In this case you can treat with the numbers as integer by multiplying them by 10 and dividing by 10 in the end.
float a, b, increment;
int a_i = 0;
int b_i = 50;
int increment_i = 1;
while(a_i != b_i)
a_i+=increment_i;
a = a_i / 10.f,
b = b_i / 10.f;
increment = increment_i / 10.f;
hey I am making small C++ program to calculate the value of sin(x) till 7 decimal points but when I calculate sin(PI/2) using this program it gives me 0.9999997 rather than 1.0000000 how can I solve this error?
I know of little bit why I'm getting this value as output, question is what should be my approach to solve this logical error?
here is my code for reference
#include <iostream>
#include <iomanip>
#define PI 3.1415926535897932384626433832795
using namespace std;
double sin(double x);
int factorial(int n);
double Pow(double a, int b);
int main()
{
double x = PI / 2;
cout << setprecision(7)<< sin(x);
return 0;
}
double sin(double x)
{
int n = 1; //counter for odd powers.
double Sum = 0; // to store every individual expression.
double t = 1; // temp variable to store individual expression
for ( n = 1; t > 10e-7; Sum += t, n = n + 2)
{
// here i have calculated two terms at a time because addition of two consecutive terms is always less than 1.
t = (Pow(-1.00, n + 1) * Pow(x, (2 * n) - 1) / factorial((2 * n) - 1))
+
(Pow(-1.00, n + 2) * Pow(x, (2 * (n+1)) - 1) / factorial((2 * (n+1)) - 1));
}
return Sum;
}
int factorial(int n)
{
if (n < 2)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
double Pow(double a, int b)
{
if (b == 1)
{
return a;
}
else
{
return a * Pow(a, b - 1);
}
}
sin(PI/2) ... it gives me 0.9999997 rather than 1.0000000
For values outside [-pi/4...+pi/4] the Taylor's sin/cos series converges slowly and suffers from cancelations of terms and overflow of int factorial(int n)**. Stay in the sweet range.
Consider using trig properties sin(x + pi/2) = cos(x), sin(x + pi) = -sin(x), etc. to bring x in to the [-pi/4...+pi/4] range.
Code uses remquo (ref2) to find the remainder and part of quotient.
// Bring x into the -pi/4 ... pi/4 range (i.e. +/- 45 degrees)
// and then call owns own sin/cos function.
double my_wide_range_sin(double x) {
if (x < 0.0) {
return -my_sin(-x);
}
int quo;
double x90 = remquo(fabs(x), pi/2, &quo);
switch (quo % 4) {
case 0:
return sin_sweet_range(x90);
case 1:
return cos_sweet_range(x90);
case 2:
return sin_sweet_range(-x90);
case 3:
return -cos_sweet_range(x90);
}
return 0.0;
}
This implies OP needs to code up a cos() function too.
** Could use long long instead of int to marginally extend the useful range of int factorial(int n) but that only adds a few x. Could use double.
A better approach would not use factorial() at all, but scale each successive term by 1.0/(n * (n+1)) or the like.
I see three bugs:
10e-7 is 10*10^(-7) which seems to be 10 times larger than you want. I think you wanted 1e-7.
Your test t > 10e-7 will become false, and exit the loop, if t is still large but negative. You may want abs(t) > 1e-7.
To get the desired accuracy, you need to get up to n = 7, which has you computing factorial(13), which overflows a 32-bit int. (If using gcc you can catch this with -fsanitize=undefined or -ftrapv.) You can gain some breathing room by using long long int which is at least 64 bits, or int64_t.
Suppose we need to generate a very long harmonic signal, ideally infinitely long. At first glance, the solution seems trivial:
Sample1:
float t = 0;
while (runned)
{
float v = sinf(w * t);
t += dt;
}
Unfortunately, this is a non-working solution. For t >> dt due to limited float precision incorrect values will be obtained. Fortunately we can call to mind that sin(2*PI* n + x) = sin(x) where n - arbitrary integer value, therefore modifying the example is not difficult to get an "infinite" analog
Sample2:
float t = 0;
float tau = 2 * M_PI / w;
while (runned)
{
float v = sinf(w * t);
t += dt;
if (t > tau) t -= tau;
}
For one physical simulation, I needed to get an infinite signal, which is the sum of harmonic signals, like that:
Sample3:
float getSignal(float x)
{
float ret = 0;
for (int i = 0; i < modNum; i++)
ret += sin(w[i] * x);
return ret;
}
float t = 0;
while (runned)
{
float v = getSignal(t);
t += dt;
}
In this form, the code does not work correctly for large t, for similar reasons for the Sample1. The question is - how to get an "infinite" implementation of the Sample3 algorithm? I assume that the solution should looks like an Sample2. A very important note - generally speaking, w[i] is arbitrary and not harmonics, that is, all frequencies are not multiples of some base frequency, so i can't find common tau. Using types with greater precission (double, long double) is not allowed.
Thanks for your advice!
You can choose an arbitrary tau and store the phase reminders for each mod when subtracting it from t (as #Damien suggested in the comments).
Also, representing the time as t = dt * it where it is an integer can improve numerical stability (i think).
Maybe something like this:
int ndt = 1000; // accumulate phase every 1000 steps for example
float tau = dt * ndt;
std::vector<float> phases(modNum, 0.0f);
int it = 0;
float t = 0.0f;
while (runned)
{
t = dt * it;
float v = 0.0f;
for (int i = 0; i < modNum; i++)
{
v += sinf(w[i] * t + phases[i]);
}
if (++it >= ndt)
{
it = 0;
for (int i = 0; i < modNum; ++i)
{
phases[i] = fmod(w[i] * tau + phases[i], 2 * M_PI);
}
}
}
I just started using boost today and found this post to be extremely helpful. I am attempting to use boost::bisect to solve a parametric equation for a series of values. The following works if I want to solve for a value of 0.8:
#include <boost/math/tools/roots.hpp>
//declare constants
const double Y_r = 0.2126;
const double Y_g = 0.7152;
const double Y_b = 0.0722;
struct FunctionToApproximate {
double operator() (double x) {
return (pow (x, 2.4) * Y_r) + (pow ((x*0.6)+0.4, 2.4) * Y_g) + (1 * Y_b) - 0.8;
}
};
struct TerminationCondition {
bool operator() (double min, double max) {
return fabs(min - max) <= t_c;
}
};
using boost::math::tools::bisect;
std::pair<double, double> result = bisect(FunctionToApproximate(), 0.0, 1.0, TerminationCondition());
double root = (result.first + result.second) / 2;
I would like to wrap this in a loop so I could solve for values other than 0.8. How would I go about this?
Many thanks!
You can add a state/data member to FunctionToApproximate to hold the value that you want to substract in each call:
struct FunctionToApproximate {
FunctionToApproximate(double val) :val(val){}
double operator() (double x) {
return (pow (x, 2.4) * Y_r) + (pow ((x*0.6)+0.4, 2.4) * Y_g) + (1 * Y_b) - val;
}
double val;
};
Then wrap the calculations inside a loop should be straight forward.
for(double i = 0.1; i <= 1.0; i+=1.0) {
std::pair<double, double> result = bisect(FunctionToApproximate(i), 0.0, 1.0, TerminationCondition());
double root = (result.first + result.second) / 2;
}
This code is supposed to generate random number between 1 to 10, but it returns 1 every time.
int random_integer;
int lowest=1, highest=10;
int range=(highest-lowest)+1;
random_integer = lowest + int(range*rand()/(RAND_MAX + 1.0));
cout << random_integer << endl;
What's wrong in the code?
If you want a random integer between lowest and highest, you'd better write
random_integer = lowest + rand() % range
You're subject to overflow here - range*rand().
Just use what regular folks use: rand() % 10 + 1.
range * rand() / (RAND_MAX + 1.0)
does not do what you think. Introduce some parens:
range * (rand() / (RAND_MAX + 1.0))
(Note that this method gives skewed distributions, though.)
I agree with all the solution provided above .
now to get a different sequence every time you run your program you can use srand() function it will provide a seed to rand() function as follows:-
srand(time(NULL))
random_integer = lowest + rand() % range
This two are always part of my programs
float randf(float lo, float hi) {
float random = ((float) rand()) / (float) RAND_MAX;
float diff = hi - lo;
float r = random * diff;
return lo + r;
}
int randi(int lo, int hi)
{
int n = hi - lo + 1;
int i = rand() % n;
if (i < 0) i = -i;
return lo + i;
}
You seem to assume that rand() returns a value between 0 and 1.
This is not correct, it return value between 0 and RAND_MAX.