Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Random number  (Read 1932 times)

0 Members and 2 Guests are viewing this topic.

Lolq

  • Newbie
  • *
  • Posts: 2
    • View Profile
Random number
« on: November 07, 2014, 03:34:52 am »
Hey guys :)
I need a little help with my random number generation for drop function.
So, here is the way i wrote it:

(click to show/hide)

and here is rand:
(click to show/hide)

The problem is that when enemy1 and enemy2 are killed at almost the same time they alway both drop item, any idea why it's like this or maybe any other way to get random number? or maybe it's just a really really really big coincidence every time? ;d

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Random number
« Reply #1 on: November 07, 2014, 03:55:42 am »
First of all, don't use rand(). Use what's provided in the random header.

Why do you seed with srand() before every call to rand()? Seed the generator just once. The way you did it, every two calls to random_drop within the same second will use the same seed and thus generate the same number.
And why on earth is that variable volatile? 

Edit: see also this post (hint: searching the forum can be useful  ;) ).
« Last Edit: November 07, 2014, 04:24:03 am by Jesper Juhl »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Random number
« Reply #2 on: November 07, 2014, 07:43:12 am »
Don't call srand more than once in your program.
Back to C++ gamedev with SFML in May 2023

Lolq

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Random number
« Reply #3 on: November 07, 2014, 12:22:37 pm »
Thanks for these answers :)
And the volatile was there just because i was trying all the pointless things that i knew won't work ;p

 

anything