Filling array with random numbers

Hi guys. I am making a game called Bejeweled, I thought I would start this by making an array with random numbers from 0 to 4 (for the 5 gems). I would then transfer this array and assign the gems to each of the numbers.
I would appreciate any ones take on why this program always returns the same values.

Code:
[
#include<iostream>
#include<string>
#include <time.h>
#include <cstdlib>
using namespace std;

// Will assign gems to have integer values for ease
int main()
{

const int triangle = 0;
const int square = 1;
const int diamond = 2;
const int circle = 3;
const int star = 4;



int board[8][8]; // This is an array to hold the information in each square on the board
int i=0;
int j=0;

while(i<8) {

while(j<8)
{
board[i][j] = rand()%5;
j++; }

i++;
}

cout << board[1][1] << " " << board[8][8] << endl;

return 0;
}
]

Outputs: 4207488 2130567168

If anyone could help, that would be awesome. Also this is all I have so far, if you have any better ideas on how to start, this could be interesting also.

I don't have a great deal of experience with C++ but do have a little with other languages. Thanks guys.
Last edited on
Even when I replace rand()%5 with say... 1. The same numbers still appear. So it isn't to do with the rand function. It is to do with the implementation of the numbers into the array.

Thanks for your swift reply. Much appreciated.
closed account (o3hC5Di1)
Hi there,

Just as a kind remark, please try to wrap your code in [ code ]-tags next time, it makes it more readable to us.

As to your problem, you are forgetting to seed the random function:
http://cplusplus.com/reference/clibrary/cstdlib/srand/

It needs to needs to be done only once, so if you call srand( time(NULL) ); after declaring the constants it should be fine.

Hope that helps.

All the best,
NwN
There is a problem with the loops. j is never reset to 0 so the inner loop will only run during the first iteration of the outer loop.

This still returns the same values. Also if I change the rand function to say 1... It still outputs the same values.

This seems highly illogical as this should make all the values in the array 1...
I have tried copy and pasting the code to another new C++ source file and build-run again but still the same values appear.

Also as a quick note, I have put square brackets around my code and I can't see a change in the post..

Thanks for your time.
Not square brackets. Code tags [code] code here [/code]
Remember that array indices starts at 0. The board only has 8x8 elements so board[8][8] is out of bounds.
closed account (o3hC5Di1)
Hi there,

Sorry, I meant code-tags - try clicking the <> button on the right of the textarea.

If you did in fact use the srand function before using the rand function, then try replacing this bit:

1
2
3
4
5
6
7
8
9
while(i<8) {

while(j<8)
{
board[i][j] = rand()%5;
j++; }

i++;
}


With this:

1
2
3
4
5
6
7
for (i=0; i<8; i++)
{
    for (j=0; j<8;j++)
    {
        board[i][j] = rand()%5;
    }
}


For-loops are a bit clearer for this kind of array-traversing.

All the best,
NwN
Last edited on
Peter87. Thank you, that was a major problem. Now one of the values is fixed. Brilliant.
I shall look into the other value now.
Thanks NwN. That looks clearer now.
I have got the whole array to fill with random numbers.
A quick question... If I define an array board[8][8], does that give me a range board[0][0] to board[7][7] or board[8][8].

I'm pretty sure it's [7][7] but just to be sure.

Thank you for all your help guys. I'm sure you will see me pop up over the next 11 weeks for my project.

If you guys ever need help with something non c++ related, i'm your man! haha
closed account (o3hC5Di1)
Hi there,

Yes, when you declare an array as such:

int board[8];

You are telling the compiler "board is an array of 8 integers".
But because arrays are zero indexed, meaning the first element is at position board[0], the last position of the element will always be max_amount-1, in your case, 8-1, board[7].

Hope that clarifies it for you.

All the best,
NwN
Topic archived. No new replies allowed.