Diori Cergy Castali (SOLVED) Random returning same value
Posts 19
Added by Diori Cergy Castali 10 months ago

Im trying to generate a lot of objects that contain randomized sprite.
But it returns the same image on all object.

the random works, but all the 70 items always have the same image (second random always same with the first).

randomItem(Item* pItem)

    srand(time(0));
    switch((rand() % 10) + 1)
    {
    case 1 :
    pItem->updateAll("Apple.png","Apple");
    break;
    case 2 :
    pItem->updateAll("Basket1.png","Basket");
    break;
    //case ...

SOLVED

the srand(time(0)) should be called once, so I put it out the loop.

Alex Zhd RE: (SOLVED) Random returning same value
Posts 70
Location Ukraine
Added by Alex Zhd 10 months ago

I think, better use the arc4random() for generating random numbers.

Phill Jones RE: (SOLVED) Random returning same value
Posts 47
Added by Phill Jones 10 months ago

arc4random() is an apple/iphone specific function isn't it?
Whereas rand() is c function

@Frizzlep


(1-2/2)