Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Random fucntion with a twist

Reply
Thread Tools

Random fucntion with a twist

 
 
alphaLaura@gmail.com
Guest
Posts: n/a
 
      01-27-2007
Hi all - I'm just new to the group and I hope nobody minds me asking
for some help.

I currently have an assignment which deals with matrices (more
specifically, Gauss-Seidel solving of matrices). One of the side tasks
we have is to generate an NxN matrix of random numbers using the rand()
function, but subject to the constraint that the absolute value of the
diagonal element (a[i][i]) must be greater than the sum of the absolute
values of all the other elements in that row. That is -

fabs(a[i][i]) > sum {from j to n} of (fabs(a[i][j])

For some reason, I have major brain block over this. My initial
thoughts were to use a do{}-while() loop, but I have since tried
everything and gotten nowhere. My random matrix-filler function
currently looks like this -

void fill_random(matrix m)
{
int i, j;
double summation = 0;

for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
m[i][j] = 2.0 * rand() / RAND_MAX - 1; // Fills element with random
number

do
{
summation += m[i][j];
break;
}
while ((i!=j) && (fabs(summation) < fabs(m[i][i])));

}
}
}


Any suggestions on this are enormously appreciated! I reckon I've just
been thinking about this for too long...

 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      01-27-2007


> Hi all - I'm just new to the group and I hope nobody minds me asking
> for some help.


What kind of C++ problem did you find?
http://www.parashift.com/c++-faq-lite/how-to-post.html


 
Reply With Quote
 
 
 
 
alphaLaura@gmail.com
Guest
Posts: n/a
 
      01-27-2007
On Jan 27, 11:07 am, "Grizlyk" <grizl...@yandex.ru> wrote:
> What kind of C++ problem did you find?http://www.parashift.com/c++-faq-lite/how-to-post.html


Aha, couldn't find an FAQ anywhere - thanks for the link. Sorry if my
topic has violated some obvious rules. I'm aware of the controversy
over asking for homework help - and I'm not after a quick solution so
I don't have to do any work!

Well, basically, runing this program gives me the following output
(bearing in mind that it is different every time due to rand());


-0.950662 0.220462 -0.696894
-0.696521 -0.43253 0.466966
0.298691 0.104779 -0.981414

For row 1, the absolute value of -0.950662 is definitely NOT greater
than the absolute values of 0.220462 and -0.696894 added together.
For row 2, the absolute value of -0.43253 is also not greater than
-0.696521 and 0.466966 added together.
For row 3, only by coincidence, is the absolute value of -0.981414
greater than 0.298691 + 0.104779.

Does that make any more sense?

 
Reply With Quote
 
Tobias Blomkvist
Guest
Posts: n/a
 
      01-27-2007
skrev:
> Hi all - I'm just new to the group and I hope nobody minds me asking
> for some help.
>
> I currently have an assignment which deals with matrices (more
> specifically, Gauss-Seidel solving of matrices). One of the side tasks
> we have is to generate an NxN matrix of random numbers using the rand()
> function, but subject to the constraint that the absolute value of the
> diagonal element (a[i][i]) must be greater than the sum of the absolute
> values of all the other elements in that row. That is -
>
> fabs(a[i][i]) > sum {from j to n} of (fabs(a[i][j])
>
> For some reason, I have major brain block over this. My initial
> thoughts were to use a do{}-while() loop, but I have since tried
> everything and gotten nowhere. My random matrix-filler function
> currently looks like this -
>

<snip>

You mean like:

void fill_random(matrix & m) {
for(unsigned row = 0; row < m.size(); ++row) {
double rowtot = 0;
for(unsigned col = 0; col < m.size(); ++col) {
m[row][col] = 2.0 * std::rand() / RAND_MAX - 1;
if(row != col) {
rowtot += std::fabs(m[row][col]);
}
}
do {
double sup = rowtot + 2.0 * std::rand() / RAND_MAX - 1;
double acc = 2.0 * sup * std::rand() / RAND_MAX - sup;
if(std::fabs(acc) > rowtot) {
m[row][row] = acc;
break;
}
} while(true);
}
}

/TB

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-27-2007

wrote:
>
> -0.950662 0.220462 -0.696894
> -0.696521 -0.43253 0.466966
> 0.298691 0.104779 -0.981414
>
> For row 1, the absolute value of -0.950662 is definitely NOT greater
> than the absolute values of 0.220462 and -0.696894 added together.
> For row 2, the absolute value of -0.43253 is also not greater than
> -0.696521 and 0.466966 added together.
> For row 3, only by coincidence, is the absolute value of -0.981414
> greater than 0.298691 + 0.104779.


- trace your program,
- print output on each for() step,
- setup matrix with concrete values instead of random values

Your code example can not be compiled and does not look like obviouse C++
error, because it is hard to understand what must be in result. Anyway,
algorithmes are offtopic here.
--
Maksim A Polyanin


 
Reply With Quote
 
alphaLaura@gmail.com
Guest
Posts: n/a
 
      01-27-2007
On Jan 27, 12:30 pm, Tobias Blomkvist <T...@127.0.0.1> wrote:
> alphaLa...@gmail.com skrev:
>
> > Hi all - I'm just new to the group and I hope nobody minds me asking
> > for some help.

>
> > I currently have an assignment which deals with matrices (more
> > specifically, Gauss-Seidel solving of matrices). One of the side tasks
> > we have is to generate an NxN matrix of random numbers using the rand()
> > function, but subject to the constraint that the absolute value of the
> > diagonal element (a[i][i]) must be greater than the sum of the absolute
> > values of all the other elements in that row. That is -

>
> > fabs(a[i][i]) > sum {from j to n} of (fabs(a[i][j])

>
> > For some reason, I have major brain block over this. My initial
> > thoughts were to use a do{}-while() loop, but I have since tried
> > everything and gotten nowhere. My random matrix-filler function
> > currently looks like this -<snip>

>
> You mean like:
>
> void fill_random(matrix & m) {
> for(unsigned row = 0; row < m.size(); ++row) {
> double rowtot = 0;
> for(unsigned col = 0; col < m.size(); ++col) {
> m[row][col] = 2.0 * std::rand() / RAND_MAX - 1;
> if(row != col) {
> rowtot += std::fabs(m[row][col]);
> }
> }
> do {
> double sup = rowtot + 2.0 * std::rand() / RAND_MAX - 1;
> double acc = 2.0 * sup * std::rand() / RAND_MAX - sup;
> if(std::fabs(acc) > rowtot) {
> m[row][row] = acc;
> break;
> }
> } while(true);
> }
>
> }/TB



Wow, that works great! Thanks But I'm afraid I'm lost with the two
most important lines of the function -

double sup = rowtot + 2.0 * std::rand() / RAND_MAX - 1;
double acc = 2.0 * sup * std::rand() / RAND_MAX - sup;

Well, more particularly, the double sup declaration. Im not sure why
we're multiplying sup and subtracting sup from acc. Is the subtraction
of sub to ensure the fabs() is not zero?

 
Reply With Quote
 
Tobias Blomkvist
Guest
Posts: n/a
 
      01-27-2007
skrev:
> On Jan 27, 12:30 pm, Tobias Blomkvist <T...@127.0.0.1> wrote:
>> alphaLa...@gmail.com skrev:
>>
>>> Hi all - I'm just new to the group and I hope nobody minds me asking
>>> for some help.
>>> I currently have an assignment which deals with matrices (more
>>> specifically, Gauss-Seidel solving of matrices). One of the side tasks
>>> we have is to generate an NxN matrix of random numbers using the rand()
>>> function, but subject to the constraint that the absolute value of the
>>> diagonal element (a[i][i]) must be greater than the sum of the absolute
>>> values of all the other elements in that row. That is -
>>> fabs(a[i][i]) > sum {from j to n} of (fabs(a[i][j])
>>> For some reason, I have major brain block over this. My initial
>>> thoughts were to use a do{}-while() loop, but I have since tried
>>> everything and gotten nowhere. My random matrix-filler function
>>> currently looks like this -<snip>

>> You mean like:
>>
>> void fill_random(matrix & m) {
>> for(unsigned row = 0; row < m.size(); ++row) {
>> double rowtot = 0;
>> for(unsigned col = 0; col < m.size(); ++col) {
>> m[row][col] = 2.0 * std::rand() / RAND_MAX - 1;
>> if(row != col) {
>> rowtot += std::fabs(m[row][col]);
>> }
>> }
>> do {
>> double sup = rowtot + 2.0 * std::rand() / RAND_MAX - 1;
>> double acc = 2.0 * sup * std::rand() / RAND_MAX - sup;
>> if(std::fabs(acc) > rowtot) {
>> m[row][row] = acc;
>> break;
>> }
>> } while(true);
>> }
>>
>> }/TB

>
>
> Wow, that works great! Thanks But I'm afraid I'm lost with the two
> most important lines of the function -
>
> double sup = rowtot + 2.0 * std::rand() / RAND_MAX - 1;
> double acc = 2.0 * sup * std::rand() / RAND_MAX - sup;
>
> Well, more particularly, the double sup declaration. Im not sure why
> we're multiplying sup and subtracting sup from acc. Is the subtraction
> of sub to ensure the fabs() is not zero?


A clearer way of writing it would probably be:

// Extension range [-1,1] to make acc greater than rowtot
// Any value would do as long as it isn't zero, even
// a static value like
// double sup = 0.333; or
// double sup = 1777;
// although then we'd loose the negative range, and vice versa
// It all depends on how greater you want acc to be compared to rowtot
double sup = 2.0 * std::rand() / RAND_MAX - 1;
// Determine sign, ie -1 or +1
double sig = std::floor(sup) + std::ceil(sup);
// Calculte
double acc = (rowtot + std::fabs(sup)) * sig;

/TB
 
Reply With Quote
 
akaustav@gmail.com
Guest
Posts: n/a
 
      01-28-2007
I am also new to this group and I'm just trying to help. Your problem
can be solved using the following function:

void fill_random(matrix m)
{
int i, j;
double summation = 0;

for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
if(i!=j)
{
// Generating the random number
m[i][j] = 2.0 * rand() / RAND_MAX -
1; // I can't understand why is this line so complicated.

// Finding the sum of all the genereated
numbers
summation += fabs(m[i][j]);
}
}
// Generate the random number for the diagonal element
over here
do
{
m[i][i] = 2.0 * rand() / RAND_MAX -
1; // Your random generation line over here.
}
while( fabs(m[i][i]) <= summation);
}
}

I hope this function works as I haven't tested it in C++. Do tell me
if this works out. I still advice
you to review your random number generator line as I don't think that
line will be able to generate
a number greater than the summation. Therefore I would suggest you to
add a random number to
your summation and write the resultant value in place of the do..while
loop.

On Jan 27, 3:39 pm, alphaLa...@gmail.com wrote:
> Hi all - I'm just new to the group and I hope nobody minds me asking
> for some help.
>
> I currently have an assignment which deals with matrices (more
> specifically, Gauss-Seidel solving of matrices). One of the side tasks
> we have is to generate an NxN matrix of random numbers using the rand()
> function, but subject to the constraint that the absolute value of the
> diagonal element (a[i][i]) must be greater than the sum of the absolute
> values of all the other elements in that row. That is -
>
> fabs(a[i][i]) > sum {from j to n} of (fabs(a[i][j])
>
> For some reason, I have major brain block over this. My initial
> thoughts were to use a do{}-while() loop, but I have since tried
> everything and gotten nowhere. My random matrix-filler function
> currently looks like this -
>
> void fill_random(matrix m)
> {
> int i, j;
> double summation = 0;
>
> for(i = 0; i < n; ++i)
> {
> for(j = 0; j < n; ++j)
> {
> m[i][j] = 2.0 * rand() / RAND_MAX - 1; // Fills element with random
> number
>
> do
> {
> summation += m[i][j];
> break;
> }
> while ((i!=j) && (fabs(summation) < fabs(m[i][i])));
>
> }
> }
>
> }Any suggestions on this are enormously appreciated! I reckon I've just
> been thinking about this for too long...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Stopping a fucntion from printing its output on screen sophie_newbie Python 4 10-18-2007 08:00 AM
How can i return an iterator from a fucntion? TOMERDR C++ 6 05-22-2006 09:21 PM
Now() Fucntion and CurrentCulture ra294 ASP .Net 5 11-26-2004 08:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57