getting multiple random nos. w/o repeated nos.

ei dudes, im kinda havin' a hard time getting random nos. for the battleship game im workn' on.
well, my prob goes like this.
i rand a number then i add up some nos. to the initial no so it would look like the nos are inter connected, when i rand the next nos. i get some chances that they have the some contradiction between the nos.(which i could not afford)

please hope you could reply as soon as possible... Really need help.. SOS.

here's the part where i needed random nos.
Last edited on
int BattleField(int&ammo)
{
//system("pause");
//system("cls");
int carrier1,carrier2,carrier3,destroyer1a,destroyer2a,submarine,
frigateA,carrierD,DestroyerAd,DestroyerBd,SubD,FrigateD;
srand(time(0));
carrier1= (rand()%100)+1;
carrierD= (rand()%3)+1;
//1=right 2=left 3=up 4=down
if((carrierD==1)&&((carrier1%10)<9))
{
carrier2=(carrier1+1);
carrier3=(carrier1+2);
}
if((carrierD==2)&&((carrier1%10)>2))
{
carrier2=(carrier1-1);
carrier3=(carrier1-2);
}
if((carrierD==3)&&(carrier1>20))
{
carrier2=(carrier1-10);
carrier3=(carrier1-20);
}
if((carrierD==4)&&(carrier1<81))
{
carrier2=(carrier1+10);
carrier2=(carrier1+20);
}
do
{
srand(1);
destroyer1a= (rand()%100)+1;
DestroyerAd= (rand()%3)+1;
if((DestroyerAd==1)&&((destroyer1a%10)<9))
{
destroyer2a=(destroyer1a+1);
}
if((DestroyerAd==2)&&((destroyer1a%10)>2))
{
destroyer2a=(destroyer1a-1);
}
if((DestroyerAd==3)&&(destroyer1a>20))
{
destroyer2a=(destroyer1a-10);
}
if((DestroyerAd==4)&&(destroyer1a<81))
{
destroyer2a=(destroyer1a+10);
}
}
while((destroyer1a==carrier1)&&(destroyer1a==carrier2)&&(destroyer1a==carrier3)&&(destroyer2a==carrier1)&&(destroyer2a==carrier2)&&(carrier3==destroyer2a));
cout<<carrier1<<" "<<carrier2<<" "<<carrier3<<"\n\n"<<destroyer1a<<" "<<destroyer2a;
Last edited on
I had to read the post five times before I understood what you meant. I recommend that you keep writing like that if your objective is to irate programmers. We are the worst kind of grammar nazis there is.

Simply use an std::set to store past numbers and check their existence after getting each, and get another one if it already exists.

Seed the PRNG just once, and if you want randomer numbers, don't seed it with a constant.
Last edited on
Topic archived. No new replies allowed.