+ 10

[SOLVED] Is there a better way to do this?

Hi! I am trying to create a formula (well I have, but am unsure if it is very good) that will be used to create an random discrete distribution based on a given set of minimum and maximum range of values. These min/max values are then fed into a random number generator. This is what I have at the moment int minRange = 5; int maxRange = 15; std::array<double, 3> distribution; // stores probability values eg. 0.50, 0.35, 0.15 int FirstMaxRange = maxRange * distribution[0]; int SecondMinRange = FirstMaxRange + 1; int SecondMaxRange = FirstMaxRange + (FirstMaxRange * distribution[1]) + 1; int ThirdMinRange = SecondMaxRange + 1; int ThirdMaxRange = SecondMaxRange + (SecondMaxRange * distribution[2]) + 1; std::discrete_distribution<int> dist{ distribution[0], distribution[1], distribution[2] }; // select which min and max values based on probability switch (dist(gen)) { case 0: return returnUniform(minRange, FirstMaxRange); case 1: return returnUniform(SecondMinRange, SecondMaxRange); case 2: return returnUniform(ThirdMinRange, ThirdMaxRange); default: //error here! return 0; } Note: the values minRange, maxRange and Distribution are usually fed from another function. they are just here for example purposes I know I lose info on conversion from double to int, (thus the +1's) This for generating stats for an rpg and I need whole numbers to be returned to the random number generator function. I am far from a math person. So I figure there has to be a better way? Or will this do? Thanks! EDIT: For clarification: i am trying to remove the hard coded values from the returnDiscrete method of randomNum class on this code: https://code.sololearn.com/c4201jw6c207/#cpp

15th May 2017, 7:51 AM
jay
jay - avatar
36 ответов
+ 8
- cause I want the player to be what ever creature they want to be also. Not just limited to the usual boring old, knight, magic user, etc etc
15th May 2017, 6:31 AM
jay
jay - avatar
+ 8
:) true. Now I need a drink! lol MORE CAFFEINATION! Thanks James for your suggestions I will definately be incorporating them into the design!
15th May 2017, 7:58 AM
jay
jay - avatar
+ 7
mmm Found a problem.. if the first percentage is lower than the second. it breaks.... boooo
15th May 2017, 6:01 AM
jay
jay - avatar
+ 7
I was thinking of basing the player and enemies off the same base class. creature. as they will all be creatures. then as you said I will base creature types off the base creature class as they will all share the base stat (slaps) system.. then finally there will be the seperation of enemy or player to handle things like movement and ai. does this sound right? i.e a player can be a dragon if they wish.
15th May 2017, 6:36 AM
jay
jay - avatar
+ 7
:) now back to mathing.. I need to change the formula as its breaks if the probabilty % of higher numbers is larger than lower numbers...
15th May 2017, 6:39 AM
jay
jay - avatar
+ 7
if you change dragons probabilites to 0.25 0.50 0.25 it does all the wrong stuff
15th May 2017, 6:41 AM
jay
jay - avatar
+ 7
Solved: FirstMaxRange = minRange + (minRange * distribution[0]); SecondMinRange = FirstMaxRange + 1; SecondMaxRange = SecondMinRange + (SecondMinRange * distribution[1]); ThirdMinRange = SecondMaxRange + 1; if(ThirdMinRange > maxRange) { ThirdMinRange = maxRange; }
15th May 2017, 7:51 AM
jay
jay - avatar
+ 6
indeed. but there is no probabilty in your example. See how lucky are you code. You have a 75% chance of getting a number under 5
15th May 2017, 5:22 AM
jay
jay - avatar
+ 6
It will be used for generating stats based on creatures type. Each creature type will have a size range, strength range etc etc And each value will have a higher or lower probability of being within a certain range. This code will be used to generate pretty much all the stats, but at the moment I have only done size.. :D
15th May 2017, 5:42 AM
jay
jay - avatar
+ 6
@james: getting there. I understand the concept pretty well I feel. This is far from complete. I only started today on this :D
15th May 2017, 5:52 AM
jay
jay - avatar
+ 6
@James. it will be so! Dragons and Orcs and Goblins oh my!
15th May 2017, 6:09 AM
jay
jay - avatar
+ 6
Nice! - I have this I done ages back - Just made it public again now, as it has problems so I took it offline, but battle will sorta work like this (but not really, it will be much more advanced). Edit: I don't want to specify creatures by name like that really. As they (the creatures) will be randomly generated for each level based on the players level and stats. There will be a boss class though with special attacks! https://code.sololearn.com/cI26Uqr56JXQ/#cpp
15th May 2017, 6:14 AM
jay
jay - avatar
+ 6
sweet!. :() class inheritance for type then huh.. so creature->dragon->enemy type of deal you mean?
15th May 2017, 6:25 AM
jay
jay - avatar
+ 5
:D I am sorry! that is not my intent! - If you want to know more about generating numbers based on probability I have a bit of code here with references to the source material. https://code.sololearn.com/cow77z70k5DX/#cpp and here is a working example (which is what I am extending upon now): https://code.sololearn.com/clIN5zIJ2oa4/#cpp
15th May 2017, 5:16 AM
jay
jay - avatar
+ 5
I prefer to use the <random> header file. It is much better at generating random numbers without bias or predictability
15th May 2017, 5:20 AM
jay
jay - avatar
+ 5
@James: It is!
15th May 2017, 5:36 AM
jay
jay - avatar
+ 5
text based for now until I get the engine working correctly
15th May 2017, 5:49 AM
jay
jay - avatar
+ 5
#Vishnu: I edited the question to add [solved] which changed the timestamp. But thanks!
15th May 2017, 8:20 AM
jay
jay - avatar
+ 3
The question was posted 18 minutes ago and you both jay and James have conversed very well. I think that typing speed of James especially is expectionally well and is quite superb. Such lengthy ones Bro. .. Keep it up! ! ! !
15th May 2017, 8:11 AM
Vishnu ks
Vishnu ks - avatar