How random is acctually the random function?

So i was messing around and testing some things and i made this absolutelly useless programm. So i wanted to learn to use the rand function and to calculate the cosine, so i made this programm which generates a random number between 0 to 180 and then outputs, the cosine value of that number. But the thing i noticed is that every time that number is larger then the previous time. So when it gets close to 180 it starts from the first number again to the top. So does anyone know if this is a somekind of bug or is there a better way to generate a random number.So that it won't output only higher then the previous.

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
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#define PI 3.14159265

bool Repeat = true;

using namespace std;

int main ()
{
    while (Repeat) {
    char symbol;
    double param, result;
    srand  ( time(NULL) );
    param = rand() % 180 + 1;
    result = cos (param*PI/180);
    cout << "Random generated number is: " << param << endl;
    cout << "The cosine of " <<  param <<  " is: " << result;
    cout << "Again? (Y or N): ";
    cin >> symbol;
    Repeat = (symbol == 'Y' || symbol =='y') ? true : false;
}
    
}  
Try calling srand() only once, and see what that does for you. Right now, you're calling it every time your run your loop. :)

-Albatross
Thanks for the tip. I moved while (Repeat) { under the srand (time(NULL) ) and now i can say for sure it's a random code.
Last edited on
Thanks for this.
Topic archived. No new replies allowed.