hey there guys. this im officially my first post on this forum. seems pretty impressive how much there is on offer, however its only C++ im looking for assistance with today.

i have two questions.

i am creating a program with lots of variables, most of which should be random numbers at the start and then get changed while the program continues, currently i just have cin commands to select them all, but it would make it easier if i could make them random numbers. is there a command to do this, and which library would i have to include?
currently i have :

``````float W1A;
float W1B;
float W2A;
float W2B;``````

then

``````cout << endl << "select each weight (random number between -1 and +1): "
<< endl << "W1A: ";
cin >> W1A;

cout << endl << "W1B: ";
cin >> W1B;

cout << endl << "W2A: ";
cin >> W2A;

cout << endl << "W2B: ";
cin >> W2B;

cout << endl << "WAC: ";
cin >> WAC;``````

2nd question, i am trying to use these numbers in a calculation and the natural exponential function doesnt seem to work, not even with math.h inlcluded,
code:

``OUTA = 1/(1 + exp^-((W1A*pix1)+(W2A*pix2)));``

if anyone can help that would be amazing,

2
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by 666kennedy

>is there a command to do this, and which library would i have to include?
The function is called rand, and you can find it in the <cstdlib> (stdlib.h if your compiler is old) header. It returns a pseudorandom number between 0 and RAND_MAX, which is usually the same as either sizeof(short) or sizeof(int). For more details about how to use it for various ranges, check out this tutorial.

>the natural exponential function doesnt seem to work
^ doesn't mean exponentiation in C++, it means binary exclusive-OR. You probably want the pow function from <cmath> (<math.h> if your compiler is ancient).

i dont want spoon fed but could you give me an example of how the "pow" command would be used.

also is this a correct use of the random command to create a random number between -1 and 1.

``float W1A = 1 + rand() % ( (-1) - 1 );``

anyway thanks again. its really appreciated.

>i dont want spoon fed but could you give me an example
>of how the "pow" command would be used.

``````#include <cmath>
#include <iostream>

int main()
{
double result = std::pow ( 2.0, 8.0 );

std::cout<< result <<'\n';
}``````

>also is this a correct use of the random command
>to create a random number between -1 and 1.
I'd go with something more like this:

``````#include <cstdlib>
#include <iostream>

double uniform_deviate ( int seed )
{
return seed * ( 1.0 / ( RAND_MAX + 1.0 ) );
}

int main()
{
for ( int i = 0; i < 20; i++ ) {
double r = uniform_deviate ( std::rand() ) * 2 - 1;

std::cout<< r <<'\n';
}
}``````

thanks, but is there not a way to just have a random number for each variable, with just one line?

is there a way to make the code i showed work how i want?

also im not sure that your example of the pow command was helpful, it might just be my limited knowledge of C++ but si there way to create the e^(x) in c++?

>is there not a way to just have a random number for each variable, with just one line?
Not if each variable is separate. You can do it with an array:

``````double a[4];

for ( int i = 0; i < 4; i++ )
a[i] = uniform_deviate ( std::rand() ) * 2 - 1;``````

>si there way to create the e^(x) in c++?
Yes, there's an exp function that does just that:

``````#include <cmath>
#include <iostream>

int main()
{
double result = std::exp ( 5.0 );

std::cout<< result <<'\n';
}``````

It's more common for people to want pow that exp (and to confuse them), so I assumed that was the case here. My bad.

thanks bud. and its ok, since i aint very good at this my questions may seem a bit ambigious. thanks for your help tho, its very much appreciated.

the exp seems like it will work now, thanks on that,

thing is my program is a little more complex as each of those "WXX" variables represents the weight of a connection between to points, and since each one is different and also needed in seperate calculations. so an array is what i want to use later but i just dont really have the knowledge to do it. so im gona try and work out how to do it the simple way first. i will put the full code once ive finished it if you would like to try and help me simplify it.

thanks so much tho, much appreciated, i did not expect so much help just hours after joining.

actually ill just put the code up. this is only the start of it, with one or two of the calculations. only on of the variables ive tried to make random, so if u want have a look and see what im obviously getting wrong.

``````//------------------------------------------------------------------

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>
#include <stdlib.h>
#pragma hdrstop

//-------------------------------------------------------------------

using namespace std;

int main ()

{
float pix1;          // selector of pattern to be taugt
float pix2 ;
float W1A = 1 + 0.1 ( rand() ) * ( (-1) - 1 );
float W1B;
float W2A;
float W2B;
double OUTA;
double OUTB;
float WAC;
float WBC;
float WBD;

cout << endl << "please select each pixel in the pattern: "
<< endl << "pixel1: " ;
cin >> pix1;
cout << endl << "pixel2: ";
cin >> pix2;

cout << endl << "select each weight (random number between -1 and +1): ";

cout << endl << "W1A: ";
cin >> W1A;

cout << endl << "W1B: " ;
//cin >> W1B;
cout << W1B;

cout << endl << "W2A: ";
cin >> W2A;

cout << endl << "W2B: ";
cin >> W2B;

cout << endl << "WAC: ";
cin >> WAC;

cout << endl << "WAD: ";

cout << endl << "WBC: ";
cin >> WBC;

cout << endl << "WBD: ";
cin >> WBD;

OUTA = 1/(1 + exp -((W1A * pix1)+(W2A * pix2)));
cout << endl << "output from neuron A: " << OUTA;

OUTB = 1/(1 + exp-((W1B*pix1)+(W2B*pix2)));
cout << endl << "output from neuron B: " << OUTB;

getch();
return 0;

}

thank you so much``````
This topic has been dead for over six months. 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.