Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone
Accepted Solution

problem with random int (Qt)

I'm using Active Frame with dynamic content

and want to show every 30 seconds some random values

 

data is inside a list

and this is how I'm getting the random ints

 

srand((uint) QTime::currentTime().msec());
int max = myList.size();
int min = 1;
int r = (qrand() % (max - min) + 1) + min;

 this works great - I can access myList.at(r-1) and use the content in my SceneCover

 

only problem is: every time I start my app again,

I'm getting the random values in the same order

 

I read that using srand with a seed value before will help to get random values in different order,

but doesn't work

 

any idea what I'm doing wrong ?

 

THX

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: problem with random int (Qt)

[ Edited ]

How did you call the seed routine? You can't pass it the same seed value each time or you'll just get the same thing. It's typical to use the system time (seconds) to seed it (if this isn't anything involving security).

 

Sorry, I didn't see that your code has the seed call at the top.

 

Are multiple threads involved? I think the seed is thread-local.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: problem with random int (Qt)

Hi!
I think qrand() requires qsrand(). srand is std C function.
It should be called once on every thread where qrand() is used.

Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: problem with random int (Qt)

[ Edited ]

Okay, so another try: you're using srand() which is from the C library PRNG, but you're calling qrand() which is from Qt. I think you need to be calling qsrand() to take into account the thread-safety aspect, which is what qrand() is doing differently.

 

Edit: Aaannd.... zmey beat me to it. :-)


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: problem with random int (Qt)

only one call from qml to C++ to get the random values

 

then in C++ in this method doing the seed and rand

 

list has 53 records, always 44,40,27 as first 3 random values

 

exit app, start app again

... same result

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: problem with random int (Qt)

thx - will try qsrand - have overlooked this ;-)

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: problem with random int (Qt)

qsrand() was the solution. THX

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: problem with random int (Qt)

Try printing the msec() value to be sure it's actually coming up different each time.

If you can verify that you're passing in a different value for the seed, yet the value returned immediately from the rand routine after is always the same, then it's certain the seeding operation is somehow failing.

Also consider printing the raw output from qrand() so you can be sure it's not your own math that's interfering, or at least confusing things.

Also try forcing the seed to some known value... if you use (say) 1337314 and I use the same, we should both get the same sequence of randoms but they won't be 44, 40, 27 or whatever they are for you now.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Highlighted
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: problem with random int (Qt)

Darn latency. :-) Nothing like trying to use a web forum in place of realtime chat!

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!