2

I want to display the shapes with dynamic position.

Ex..

     A      B      C     D
     D      A      B     E
     E      B      D     A

Here the coding:

string names[] = {
"AB",
"ABC",
"ABCZ",
"ABCDEDF",

};

void HelloWorld::addShapes(HelloWorld* game)
{
name = names[arc4random()%4];
......

.....

CCPoint pos1[8];
for (int i = 0; i< TempNumOne; i++)
{
    pos1[i]=CCPoint(disx, disy);
}


for (int a=0; a<TempNumOne; a++)
{
    Filename[a]=FileMeasure[a];
    int temp= arc4random()%TempNumOne;
    ......
    bodyDef.position.Set(pos1[temp].x/32.0f, pos1[temp].y/32.0f);
    .....

    switch (Filename[a])
    {
        case 'A':
        {
           ......
        }
        case 'B':
        {
            ......
        }
        etc.....
     }

All the logic working fine except dynamic position.

Sometime arc4random function returns the same values in the looping statement. I have same position for two shapes.

I want to display the shapes different position.

Can any one assist me?

Vanarajan
  • 539
  • 1
  • 4
  • 19
  • Why do you need pointer to game? It isn't the same to this? Also try to don't reuse your code like in your switch. Lines "game->addBodyNode(nodehead, 1); nodehead->retain()" you should move to after switch. It's bad programming habit, because your code is longer and harder to read. It isn't solve your problem but make your code clean and more readable. – Wez Sie Tato Nov 12 '13 at 07:57
  • @WezSieTato: I have noted your point. Thanks – Vanarajan Nov 12 '13 at 09:19

2 Answers2

1

You can't use random that way. It may return the same values (that's how random works). What you need is random_shuffle

std::string[] names = {"A", "B", "C"};
std::random_shuffle(std::begin(names), std::end(names));
//now names are in random order. just iterate over them.
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
Andrew
  • 24,218
  • 13
  • 61
  • 90
  • thanks a lot. The "std::random_shuffle(pos1,pos1+count)" is shuffle syntax support objective c++. – Vanarajan Nov 12 '13 at 09:10
  • @Vanarajan: it's not objective c++ syntax. `std::begin/end` return container specific iterators, which are just plain pointers in case of array. Thats why your version works – Andrew Nov 12 '13 at 12:09
0

If you want unique random numbers you need to implement it... You can remember all previous numbers and check if next number is unique, but its non-deterministic algorithm. Or use better algorithm like Knuth-Fisher-Yates algorithm: Unique (non-repeating) random numbers in O(1)?

Community
  • 1
  • 1
Wez Sie Tato
  • 1,186
  • 12
  • 33