This function is suppose to randomly chose 2 out of 16 dice, swap their positions, and then chose what side of the dice is facing up. The problem is, when I run the function (I loop it 250 times). It only changes a few dice and the board looks close to the original. I'm not sure if I'm doing something wrong.

void Shake(int Dice[], int Side[])
{
	int Temp1, Temp2;
	
	srand(unsigned(time(NULL)));
	Temp1 = Dice[(rand()%16)];
	Temp2 = Dice[(rand()%16)];
	while (Temp1 == Temp2)
	{
		Temp2 = Dice[(rand()%16)];
	}
	Dice[Temp1] = Temp2;
	Dice[Temp2] = Temp1;
	Side[Temp1] = (rand()%6);
	Side[Temp2] = (rand()%6);
}

Recommended Answers

All 8 Replies

You have some value index confusion happening here I think

Try

int temp;
int idx0 = rand() % 16;
int idx1 = idx0;
while( idx1 == idx0 )
    idx1 = rand() %16;
temp = Dice[idx0];
Dice[idx0] = Dice[idx1];
Dice[idx1] = temp;

>>srand(unsigned(time(NULL)));

You only need to do this once. Put that statement inside main, and just
call it once. It only needs to be called once.

> (I loop it 250 times)
...
> srand(unsigned(time(NULL)));
Unless your code also takes 250 seconds to run, then srand() is always going to be seeded with the SAME value, and rand() is always going to return the SAME value(s) as a result.

srand() needs to be called ONCE only, at the start of main.

> (I loop it 250 times)
...
> srand(unsigned(time(NULL)));
Unless your code also takes 250 seconds to run, then srand() is always going to be seeded with the SAME value, and rand() is always going to return the SAME value(s) as a result.

srand() needs to be called ONCE only, at the start of main.

Although I think it could be called before main.

This function is suppose to randomly chose 2 out of 16 dice, swap their positions, and then chose what side of the dice is facing up. The problem is, when I run the function (I loop it 250 times). It only changes a few dice and the board looks close to the original. I'm not sure if I'm doing something wrong.

void Shake(int Dice[], int Side[])
{
	int Temp1, Temp2;
	
	srand(unsigned(time(NULL)));
	Temp1 = Dice[(rand()%16)];
	Temp2 = Dice[(rand()%16)];
	while (Temp1 == Temp2)
	{
		Temp2 = Dice[(rand()%16)];
	}
	Dice[Temp1] = Temp2;
	Dice[Temp2] = Temp1;
	Side[Temp1] = (rand()%6);
	Side[Temp2] = (rand()%6);
}

Note check the srand(unsigned(time(NULL))) i guess when this is used for random numbers, you must also use the " #include<time.h> " header file to use whatever function is inside of it. i think this might help **************

Although I think it could be called before main.

Impossible. Nothing can be called before main()

Impossible. Nothing can be called before main()

Nit: that's not exactly true.

#include <iostream>
#include <cstdlib>
#include <ctime>

class T
{
public:
   T()
   {
      std::cout << "T ctor\n";
      srand(time(0));
   }
};

T obj;

int main()
{
   std::cout << "hello world\n";
   return 0;
}

/* my output
T ctor
hello world
*/
commented: appreciate! +6

> (I loop it 250 times)
...
> srand(unsigned(time(NULL)));
Unless your code also takes 250 seconds to run, then srand() is always going to be seeded with the SAME value, and rand() is always going to return the SAME value(s) as a result.

srand() needs to be called ONCE only, at the start of main.

Yep, and remember that srand seeds off of the system clock every SECOND. Your program will loop through many times per second and all the random numbers will be seeded with the same number, which is why the dice are not diff values

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.