Random Numbers in C++.

Jan 15, 2012 at 9:28am
Hello all! First time poster, and VERY new C++ user. I'm just learning the ropes here, so be gentle. =)

Anyway, here's what I've set out to do: I'd like to have "randomized" binary numbers appear in a manner such as a computer screen (format wise, so that shouldn't be an issue...). I did a quick search and found http://www.cplusplus.com/forum/windows/13326/ , but it didn't quite solve my problem, and it didn't even run for me, so I've done something wrong, methinks. I'm using Code::Blocks, if it matters (don't think it does, but I'm just putting it out there).

Thanks in advance!
Jan 15, 2012 at 12:28pm
This will compile in Code::Blocks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> //Input/output functions.
#include <ctime> //Contains the time functions we need.
#include <cstdlib> //Contains the random functions we need.

using namespace std;

int main()
{
    bool nums[100]; //Declare an array of bool values.
    short int i = 0; //We will use this to iterate around our loop.
    srand(time(NULL)); //Set the seed for the random numbers to the current time (so we get a different seed each time we run the program).
    while(i < 100) //Iterate through the array. 
    {
        nums[i] = rand() % 2; //Make our bool value a random number from 0-1.
        cout << nums[i]; //Output number.
        ++i; //Carry on through the loop.

    }
    return 0;
}


I would recommend reading:
this: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

and this: http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

Jan 15, 2012 at 6:34pm
Everything you've listed makes sense, Mats, but I'm getting 11 errors in Code::Blocks when I attempted to run this. I've copy pasted this exactly, so this isn't an issue.

Line 1, error: iostream: No such file or directory
Line 2, error: ctime: No such file or directory
Line 3, error: cstdlib: No such file or directory
Line 5, error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
In function 'main':
Line 9, error: 'bool' undeclared (first use in this function)
Line 9, error: (Each undeclared identifier is reported only once
Line 9, error: for each function it appears in.)
Line 9, error: expected ';' before 'nums'
Line 11, error: 'NULL' undeclared (first use in this function)
Line 14, error: 'nums' undeclared (first use in this function)
Line 15, error: 'cout' undeclared (first use in this function)

These are the errors I'm getting, so I'm assuming I've done something wrong with Code::Blocks itself, considering it's not recognizing the libraries. When opening Code::Blocks, I went New --> Empty File. Then I input the data on that file, and compiled. What am I doing wrong?
Jan 15, 2012 at 6:50pm
Are you sure you compile it as C++ and not C? If your file name ends with .c I think Code::Blocks try to compile the code as C. Make sure your file name ends with .cpp or some other common C++ file extension.
Jan 15, 2012 at 8:59pm
Peter87, you're right. It was saving as a C file. After saving the file as a .cpp, it compiles with no errors, thanks!

Mats code works just as I had hoped, with one exception: it ends far shorter than I had hoped. Ideally, I would like it to fill the command prompt with randomized binary numbers, but it ends after filling the first one and a third lines. Is that due to "while(i < 100)" (line 12)'s code, telling it to only do 100 lines of code?
Jan 16, 2012 at 6:25am
i < 100 tells it to do 100 numbers. If you want x numbers you need to change:
1
2
3
bool nums[100] //That 100 to x

i < 100 //That 100 to x 


Also note that if you want to go above 32000, you will have to delete 'short'.

But you would know all this if you read about arrays and control loops. You REALLY need to know that stuff to do anything in C++.
Topic archived. No new replies allowed.