0

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.

3
Contributors
6
Replies
45
Views
1 Year
Discussion Span
Last Post by ALosh99
Featured Replies
  • 1

    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 … Read More

  • 1

    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 … Read More

1

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.

0

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

srand(time(NULL)); 
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 ?

Edited by ALosh99

0

or like this:

std::srand(std::time(NULL)); 
int random1 = std::rand();
int random2 = std::rand();

which one you think is better/correct

1

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 
  srand(time(NULL)); 

 // 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!)

0

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

 Random random(0,1);   
    random.seed(2);   

It is work with me,thanks anyway

Edited by ALosh99

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.