Question

Here is the code I am referring to:

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 << "in: " << n1 << ".0 " << n2 <<  ".0 " << endl;
            cout << "out: " << t << ".0" << endl;
        }
    }
}

It seems these two lines are the same save for n1 and n2:

int n1 = (int) (2.0 * rand() / double(RAND_MAX));
int n2 = (int) (2.0 * rand() / double(RAND_MAX));

But when I cout (int) (2.0 * rand() / double(RAND_MAX)) the answer always seems to come out as 1.

So how could this line be true:

int t = n1 ^ n2; // should be 0 or 1

The output of the code shows it to be i/e:

in: 1.0 0.0 
out: 1.0
in: 0.0 1.0 
out: 1.0
in: 1.0 0.0 
out: 1.0
in: 0.0 1.0 
out: 1.0
in: 0.0 0.0 

But (1 ^ 1) as in t = n1 ^ n2 always cout's as 0. So how is the above output possible? Or am I just not cout-ing these lines enough (I did each one many times, like 10 each).

Edit What I don't understand is every time I both compile and run the line cout << (int) (2.0 * rand() / double(RAND_MAX)); it outputs a 1 and n1 and n2 both equal (int) (2.0 * rand() / double(RAND_MAX)); so how could t = n1 ^ n2 ever come out to 1? Yet there it is.

Was it helpful?

Solution

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

OTHER TIPS

Edit what I don't understand is every time I both compile and run the line cout << (int) (2.0 * rand() / double(RAND_MAX)); it outputs a 1 and n1 and n2 both equal (int) (2.0 * rand() / double(RAND_MAX)); so how could t = n1 ^ n2 ever come out to 1 yet there it is

The first time you call it, it outputs a 1.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  for (int i = 0 ; i < 10; ++i)
  {
      int j = (2.0 * rand () / double (RAND_MAX));
      printf ("%d\n", j);
  }
}

Gives:

1
0
1
1
1
0
...

Since your implementation of rand may differ, so may your output.

Because the random number generator starts out in the same state each time you run the program, it produces the same first result each time. But calling rand changes the state, so the next call to rand from the same instance can produce different output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top