C++ Random doubles from -1 to 1 [duplicate] - c++

This question already has answers here:
generate random double numbers in c++
(10 answers)
Closed 4 years ago.
I need to randomly generate numbers (using random()) from -1 to 1 that are doubles. For example the output would be something like:
1,
-0.3324,
0.7821,
0.9823,
-0.111
etc... this is what I was trying to do walk_Length = (double)rand()%2 - 1;

You might get away with something like
double walk_Length = static_cast<double>(rand()) / RAND_MAX * 2 - 1;

Related

Getting a weird result [duplicate]

This question already has answers here:
Display a number decimal format instead as an exponential in cout
(2 answers)
Closed 2 years ago.
float k,n,s,p;
float a,d;
cin>>k>>n>>s>>p;
a=n/s;
a=ceil(a);
d=k*a;
d=d/p;
cout<<ceil(d);
Am getting an output = 1e+008 when the inputs are 10000, 10000, 1, 1.
The actual result should be 100000000.
I tried using double instead of float but no success.
Kindly throw some light
Use std::fixed
cout<<fixed<<setprecision(8)<<d<<endl;

How to fix rand()/RAND_MAX in a method that always produces 0.0000000? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Integer division always zero [duplicate]
(1 answer)
Random number c++ in some range [duplicate]
(6 answers)
Closed 3 years ago.
I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.
I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.
void cPortfolio::randomize(cProblem &portfolioProblem) {
int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
int test;
for (int i = 0; i < assetCount; i++) {
double num = rand() / RAND_MAX; //this always produces 0.0000
int test = num * (portfolioProblem.assetNum); } `} //cannot format these correctly please ignore the brackets

What is the internal functioning of the following code? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.

How to Set certain digits for generating random numbers in C++ [duplicate]

This question already has answers here:
How do I scale down numbers from rand()?
(9 answers)
Closed 8 years ago.
Hi. Is there any way to set the size of random numbers?( in random number generator "rand()")
For example I want to generate 10 digits random numbers.
and one more question, how can i set random function to generate numbers between 0 and 1 (for example 0100110110) ?
Im not sure about setting the size of the numbers. However I dont think it would be possible to get each digit to produce just a 0 or 1.
What you can do however is something like below:
ostringstream 10digitNumber;
for(int i = 0 ; i < 10 ; i ++){
v1 = rand() % 2;// generate o or 1
10digitNumber<< v1;// build up a string of 1 and 0
}
int real10DigitNumber = static_cast<int>10digitNumber); // typecast to integer
Please forgive me if my syntax isn't 100 %. Its being awhile since I used c++.

How to quickly determine how many digits in an int? [duplicate]

This question already has answers here:
Finding the length of an integer in C
(29 answers)
Closed 9 years ago.
I know I can do:
n = floor(log10(i)) + 1;
Or I can do a quick loop:
while(i) {
n++;
i/=10;
}
Is there any better way than a complicated math operation, or a loop to achieve the goal? For example: if i = 1234, then n = 4.
The shortest way I know of (not computationally, just in terms of typing) is to call snprintf(3):
int n = snprintf(NULL, 0, "%d", i);
convert it to a string (itoa) and count the number of characters? (might be not the best performance-wise though)