How can we insert two independent random number to two equations (x,y) contain two parts like this:
x = a* randomnumber1 + b* randomnumber2
y = c* randomnumber1 + d* randomnumber2
these two random number should be independent.
Thank inadvance.

Recommended Answers

All 6 Replies

How are you generating your random numbers? Show your code please. Assuming you have assigned a random number (usually using the rand() function if C or C++) to randomnumber1 and randomnumber2, then your computation should be correct. You initialize your random number generator with a seed value - often the local time value, and then each call to rand() will produce a new (independent) value.

Thank you for your reply , I am using c++
I wrote it like this :

float   Random1 = (float(rand())/(RAND_MAX)) + 1
float   Random2 = (float(rand())/(RAND_MAX)) + 1
x = a* random1 + b* random2 
y = c* random1 + d* random2

is this correct ?

or like this:

int random1 = std::rand();
int random2 = std::rand();

which one you think is better/correct

Well done.

Your code is nearly correct but I am not 100% sure that it is giving your what your want:
So here is your code with some comments -- just to make sure:

  // This line sets the random number seed. 
  // you only need this call once only in your code 

 // This is strange :  -- your code produces a random
 // number between 1.0 and 2.0. This is because rand() returns an 
 // integer between 0 and RAND_MAX so the division gives you 
 // a number between 0.0 and 1.0  and then you add 1. 
 float   random1 = (float(rand())/(RAND_MAX)) + 1;
 float   random2 = (float(rand())/(RAND_MAX)) + 1;

// Now this is fine
x = a* random1 + b* random2; 

// this line is strange becauses random1 and random2 have not changed.
// I expected to see ANOTHER copy of this:
random1= float(rand())/MAX_RAND +1.0;
 y = c* random1 + d* random2;

If you test your code with* a and *b then same value as c and d, I expect you want a different value for x and y.

(note : don't forget your semi-colons!)

Thank you for your explanation.
I have used generates Pseudo random numbers like this :

 Random random(0,1);   

It is work with me,thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.