rand between -x and x

I everybody, I'm searching for a method to use rand()and srand() to have a randomic number between -x and x.
x is a normal double variable... how can I solv it?
I sow that in C you can use seed...
between -x and x

Do you mean [-x, x] or [-x, x) or (-x, x] or (-x, x)?

http://www.cplusplus.com/reference/random/uniform_real_distribution/
Does it need to be using rand(), or are you only saying that because that's what you're familiar with?

I second using uniform_real_distribution, it's much easier to work with, despite the slightly more complicated setup.
See the example in keskiverto's link.
Last edited on
Something simple like
rand() % (2*x) - x;

Adjust with some +/-1 depending on whether you want open or closed intervals.
OP's question was for reals, so you'd need to incorporate RAND_MAX in there to normalize it, if you need to go that route.

But note that this comes with trade-offs:
http://www.azillionmonkeys.com/qed/random.html
Last edited on
Indeed. Standard C rand() is only good enough for student coin toss homework.

"Randomness is too important to be left to chance".
http://www.quotationspage.com/quote/461.html
The C standard recommends not using rand if there are alternatives available. (C11 standard, note 295)

C++ certainly does have that.

A bit out-dated and long-winded, but the issues raised are still valid about avoiding the use of rand:
https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

std::random device is more robust than rand for most trivial cases and is as easy to use when instantiated. No need to seed the bugger.

The C++ random engines and distributions look "scary" because they offer a lot more options vs. rand. Most programming courses don't cover <random> (or <chrono>).

Random number generation, from C to shining C++:
https://www.learncpp.com/cpp-tutorial/59-random-number-generation/
Hi everybody, thanks all for the answers.
I should do it only for university homework so it isn't "important"...
I'm a beginner and I want to make a program where a variable takes a value from a random number between -x and x.
So if I have
double x = 5.0;
What I want is
double y = rand();
And y should have a value between -5.0 and 5.0 .... so -4.2 || 0.0 || 3.8 ecc...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

double u01()
{
   string str = "0.";
   for ( int i = 0; i < 14; i++ ) str += "0123456789"[rand() % 10];
   return stod( str );
}

int main()
{
   int N = 10;
   srand( time(0) );

   int x;
   cout << "Input x: ";   cin >> x;
   cout << N << " random numbers in [" << -x << ", " << x << ") are:\n";
   while( N-- ) cout << -x + 2 * x * u01() << ' ';
}


Input x: 5
10 random numbers in [-5, 5) are:
-1.83806 -2.27038 3.10626 3.85746 -1.06539 -3.0556 -4.70587 1.08927 4.3542 1.82575 

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <random>

int main()
{
   using std::cout;
   int N = 10;
   int x;
   cout << "Input x: ";   std::cin >> x;
   cout << N << " random numbers in [" << -x << ", " << x << ") are:\n";
   std::default_random_engine generator;
   std::uniform_real_distribution<double> distribution( -x, x );
   while( N-- ) cout << distribution(generator) << ' ';
}


I should do it only for university homework so it isn't "important"...

Isn't it important what one learns in university?
1
2
3
4
5
6
double u01()
{
   string str = "0.";
   for ( int i = 0; i < 14; i++ ) str += "0123456789"[rand() % 10];
   return stod( str );
}
This is creative.
But it definitely shouldn't be used in anger!
@keskiverto,
Isn't it important what one learns in university?

Not if it is some very old stuff <= C++98
A university/school teaching C++11 or later? Especially to beginners?!? WHOA! That is a radical idea!
My university is currently teaching C++14, but they allow me to submit code written in C++17 or some C++20 features since they are running the latest version of g++ provided I give the exact compilation instructions or makefile
That is not the norm for most schools or universities. The curriculum is very often years out of date, usually taught by people who were trained by a curriculum just as old. If not older.
Learner2 wrote:
[quote=keskiverto]Isn't it important what one learns in university?

Not if it is some very old stuff <= C++98[/quote]
Exactly. One does probably pay (time and money) for studying in university.

Is it ok for a shop to sell food that has "Best before" date long gone?
Is it wise for a customer to waste resources (money) on worthless products?

I deem both sides guilty.
Topic archived. No new replies allowed.