-2

Ok I have a question about the Random Number Generator. However, let me explain I know how it works. So there is going to be minimal code. My question is what I call a floating index. This would be used as dice style generator. So I will explain the game example and then a piece of code.

My father is wanting to create a DnD style game. With Attack and defense. The random number generator is the dice. However with each player will have a +Attack bonus and so forth. Kind of like Risk.

My question is when the index of say (1,6) with a attack bonus of (3). The random number generator is (1,6). Can I float that index by the players attack damage on the roll.

So for example one player may have (1,6) and another player could have (1,20). As they level up those numbers will change.

So can I use an int variable in a random number generator as follows.

Val X = 0
Val y = playerAttack.tostring
 
....... (X , y)

So basically if a random number generator is (1,6) then player two with (1,20) will throw an out of index.

So my question is can I float that number to auto change the index based on the player stats.

Think of the game Dungeon and Dragons. Or would I have to do the generator as (1, 100) and the use of else statements to limit the number in the index.

I am needing clarification because I am under the assumption that I would need several random number generators with different indexes.

Any help would be greatly appreciated.

Camp Nerd
  • 327
  • 1
  • 11
  • 1
    "The random number generator is the dice." I think you might have an issue with the way you're modelling your problem. You can use one random-number generator for the whole game, and simply provide it with different indices as the game progresses – bumbread Apr 16 '23 at 12:03
  • "can I float that number to auto change the index"? – njzk2 Apr 16 '23 at 12:09
  • @bumbread that's what I am trying to figure out. Is how to press one button for the dice and everything be calculated. When I learned how to make the Random Number Generator in C# it was (1, 10) for example. However when you have a fighter game how would increase the indices based off the stats that you can upgrade. – Camp Nerd Apr 16 '23 at 12:11
  • @njzk2 that may have been a poor choice of word, however think of an index of (1,7) on one roll and the next roll it changes (1,20) all using one Generator. – Camp Nerd Apr 16 '23 at 12:13
  • I'm not sure what you mean by index? do you refer to the random generator boundaries? – njzk2 Apr 16 '23 at 12:54
  • @njzk2 if you have two players. (Think street fighter or a boxing game) for hit damage would be on one player scale of 1-20 and the other 1-10.. if the value of the scale of random number is only 1-10.. without using multiple generators how can I make the 1-10, change to 1-20, the scale is based on player stats. So would I need player one Generator and player two generator... What if a million people are playing. Do I have a generator with a million different combinations. How would do all this on one Generator or do I need multiple generators – Camp Nerd Apr 16 '23 at 13:56
  • does this answer your question? https://stackoverflow.com/questions/45685026/how-can-i-get-a-random-number-in-kotlin – njzk2 Apr 16 '23 at 16:05
  • Most `random` functions can take a range of values, so why not call e.g. `nextInt(1, currentMax + 1)` for a player? You supply the range, and the generator gives you a random value within that range. If you always supply `currentMax` then as that value changes, the range of possible values changes too. There's no need to keep that *"maximum value"* state in the generator itself - and that's especially important if it's going to be shared among lots of players, where you could end up with race conditions (player X sets a new max, and before it can request a value, player Y sets a different max) – cactustictacs Apr 17 '23 at 19:11

1 Answers1

0

Not sure if I understand what you mean, but I think this might be what you're after.

data class Player(var attackPower: Int)

fun generateAttackValue(player: Player): Int {
    return (1..player.attackPower).random()
}

You can use this same number-generating function for any player no matter what their attack power is.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • that's what I was thinking originally, however, I am working on a few concepts with the code on preliminary portion of the App.. also the concept keep changes so I am just trying to get a formal coding idea then start testing the concept. – Camp Nerd Apr 18 '23 at 21:04
  • So this doesn't answer your question as aksed? – Tenfour04 Apr 19 '23 at 00:37
  • I think it does but I am not sure until I put it in code.. right now we have everything on paper and rolling a physical dye Odds are missing and even is a hit.. and we been subtracting the numbers on paper. So we are putting into an app.. once I get to the portion of coding if it answers my question then I will mark it.. As of right now we are still tweaking it on paper before we put it digital – Camp Nerd Apr 19 '23 at 18:12
  • you also have what you posted plus you have Blocks, Battery consumption, armor depletion and so forth. So we are getting the base mechanics down.. so I am assuming I will have about 5 different random number generators. – Camp Nerd Apr 19 '23 at 18:14