473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Random fucntion with a twist

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...

Jan 27 '07 #1
7 1861

al********@gmail.com
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
Jan 27 '07 #2
On Jan 27, 11:07 am, "Grizlyk" <grizl...@yandex.ruwrote:
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?

Jan 27 '07 #3
al********@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

Jan 27 '07 #4

al********@gmail.com 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
Jan 27 '07 #5
On Jan 27, 12:30 pm, Tobias Blomkvist <T...@127.0.0.1wrote:
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 :D 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?

Jan 27 '07 #6
al********@gmail.com skrev:
On Jan 27, 12:30 pm, Tobias Blomkvist <T...@127.0.0.1wrote:
>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 :D 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
Jan 27 '07 #7
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...
Jan 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
5
by: ra294 | last post by:
When I use the Now() Fucntion i my ASP.net application I get a date format of mm/dd/yy. I want it to be dd/mm/yy. I set in the web config "culture="en-GB" uiCulture="en" and also set the regional...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
1
by: Haggr via AccessMonster.com | last post by:
"My Fucntion()" will return a string value of lxb114 for a query field with a string of xlb114r. So my question is how to text that Function in the immediate window of the module? Tried...
6
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
2
by: sowmi | last post by:
Hi All, I'm learning Pointers to functions in C.But I have some doubts regardign the same.I would be v.happy and thankful if somebody can help me out on my pbm The problem is mentioned below:...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
4
by: Chuckhriczko | last post by:
I need to possibly twist an image on the z axis in Javascript. Here is the deal. We have sliding effects and so forth for our company's website but there is too much sliding. We want something more...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.