Varying randomness

0 favourites
  • 9 posts
  • Hi everyone,

    I'm currently stumped again, I know what the problem is, I just don't know how to fix it. Basically I have a turret which currently has 4 different types of ammo in it and if any of the ammo is greater than zero it would fire based on it being random. My first solution is like this.

    <img src="http://dl.dropbox.com/u/57137706/sol1.png" border="0" />

    But the problem is because it chooses the ammo first, there are times when there is a gap between firings because it is choosing a random number with no ammo in it. So the way to fix this was to choose what ammo was available first, then choose a random number from that seed. For example if ammo1 and ammo2 was greater than 0 then the random seed would be 2, if all 4 ammo types is greater than 0 then the random seed would be 4 but I don't know how to link up the random chosen number with the ammo types. Fixed numbers wouldn't work because the random seed can change.

    Here is the capx

    dl.dropbox.com/u/57137706/variable%20random.capx

    Just disable group 2 and enable group 3 if you want to see the old method.

    Thank you for your time everyone.

    Kind Regards.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'd like to know a good answer to this also.

    You could use 'Repeat' to keep generating a new number if that ammo type is at 0, but that would be a sh*tty solution.

  • Sorry keepee, but I don't understand how that would fix the problem could you elaborate please?

  • Sorry all, just giving this a bump incase anyone that can help missed it.

  • What you want, then, is a random shuffle of certain elements.

    Keepee's solution would work however he is right in the fact that it is not the most efficient solution.

    What I think you might need to do is fill out a 1-dimensional array beforehand, either manually or procedurally, then shuffle it.

    Shuffling the array is not simple, but here's a nice example of it being done.

    I might have missed a better way of doing it, but I think this will work if done right.

  • Many thanks Sqiddster, I will give it a shot. I tried to avoid arrays for my first project but I guess there's no helping it.

  • Here's what I came up with. It looks unnecessarily complicated haha.

    Local text [b]ammoString[/b] = ""
    
    System: Set [b]ammoString[/b] to [i]Sprite.ammo1 > 0 ? ammoString & "1" : ammoString[/i]
    System: Set [b]ammoString[/b] to [i]Sprite.ammo2 > 0 ? ammoString & "2" : ammoString[/i]
    System: Set [b]ammoString[/b] to [i]Sprite.ammo3 > 0 ? ammoString & "3" : ammoString[/i]
    System: Set [b]ammoString[/b] to [i]Sprite.ammo4 > 0 ? ammoString & "4" : ammoString[/i]
    
    System: Set [b]randomNumber[/b] to [i]int(mid(ammoString, floor(random(len(ammoString))), 1))[/i]
    

    Basically create a string with the available ammos eg. "134" and then choose a random character from that string.

    randomAmmo.capx (r101)

  • Once again thank you ramones, that's a pretty clever way to solve this problem. I've just been using number values, didn't know you could use strings like that.

  • Pattern gen plugin might help you to get shuffling result in some cases.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)