The random number generator rand()
will always give you 1 first time you run program.. if you don't want to make it always give 1 the first time then set the randon number generators seed to a different value because right now it always uses the same default seed. call the command srand()
to change seed value, you could make it more random everytime by setting the random number generators seed to your computer clock that way it would look more random
using this code
#include <ctime>
srand((unsigned)time(0));
I see nothing wrong with it, I made it on 1 line maybe your eyes just playing tricks on you, http://ideone.com/WaMihm
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
// Random training sets for XOR -- two inputs and one output
cout << "topology: 2 4 1" << endl;
for (int i = 2000; i >= 0; --i) {
int n1 = (int) (2.0 * rand() / double(RAND_MAX));
int n2 = (int) (2.0 * rand() / double(RAND_MAX));
int t = n1 ^ n2; // should be 0 or 1
cout << "[A]in: " << n1 << " [B]in: " << n2 << " out: " << t << endl;
}
}
.
Output:
topology: 2 4 1
[A]in: 1 [B]in: 0 out: 1
[A]in: 1 [B]in: 1 out: 0
[A]in: 1 [B]in: 0 out: 1
[A]in: 0 [B]in: 1 out: 1
[A]in: 0 [B]in: 1 out: 1
[A]in: 0 [B]in: 1 out: 1
[A]in: 0 [B]in: 1 out: 1
[A]in: 1 [B]in: 1 out: 0
[A]in: 1 [B]in: 1 out: 0
[A]in: 0 [B]in: 1 out: 1
[A]in: 0 [B]in: 0 out: 0
[A]in: 0 [B]in: 1 out: 1
[A]in: 0 [B]in: 0 out: 0
[A]in: 0 [B]in: 0 out: 0
[A]in: 1 [B]in: 0 out: 1
[A]in: 1 [B]in: 1 out: 0
[A]in: 1 [B]in: 0 out: 1
[A]in: 1 [B]in: 1 out: 0
[A]in: 0 [B]in: 1 out: 1
[A]in: 0 [B]in: 1 out: 1
[A]in: 1 [B]in: 1 out: 0
[A]in: 0 [B]in: 1 out: 1
[A]in: 0 [B]in: 0 out: 0
[A]in: 1 [B]in: 1 out: 0
[A]in: 0 [B]in: 1 out: 1
[A]in: 1 [B]in: 0 out: 1
[A]in: 0 [B]in: 1 out: 1
[A]in: 1 [B]in: 0 out: 1
[A]in: 0 [B]in: 0 out: 0
[A]in: 0 [B]in: 0 out: 0
[A]in: 0 [B]in: 1 out: 1