24 Replies - 15970 Views - Last Post: 24 April 2011 - 02:10 PM
#1
Random number probability
Posted 19 April 2011 - 01:55 PM
I'm making a game with a friend, sort of a text based RPG, and we are doing an attack function. The damage dealt is going to be a random number between 1-60 (subject to change), what i really wanted to do is make it kind of like a bell curve, 30 being the middle, and making it decrease in likeliness of getting that number the closer it gets to 60, and the closer it gets to 0. Is there a good way to do it? I guess kind of picture a bell curve, 30 is the mean and it gradually decreases as the number nears 30.
Is there a good way to do this? I have sat down with a few of my friends and we have not figured out a way to do it.
I can put up some code of how the damage thing works right now, but it is very simple, just a random number generated between 1-60, and then it subtracts that from the players health.
Thanks,
Kyle_vdk
Replies To: Random number probability
#2
Re: Random number probability
Posted 19 April 2011 - 02:18 PM
Maybe that's not easy to understand, but think of it like this, lets say you roll two dice. How many ways are there to make 1? 0 ways. How many ways are their to make 2? 1 way. how many ways are their to make 3? 2 ways.. etc.. all the way up to 7, where it peaks, then starts decreasing again.
Compile and run this to see what i mean. Sorry, i know it's a crude example.
#include <iostream>
#include <ctime>
int main() {
srand((unsigned)time(0));
int vals[60];
memset(vals, 0, sizeof(int)*60);
for (int i = 0; i < 10000; i++) {
int random_integer1 = (rand()%30);
int random_integer2 = (rand()%30);
int total = random_integer1+random_integer2;
vals[total]++;
}
for (int i = 0; i < 60; i++)
std::cout << vals[i] << " ";
std::cin.ignore();
std::cin.get();
return 0;
}
Sample output
Quote
64 223 317 292 348 288 293 353 329 319 291 288 269 262 243 26
65 173 174 178 131 134 124 100 110 91 90 67 48 44 37 21 10 0
EDIT: Changed the code to generate numbers between 0 and 60, rather than 0 and 12.
This post has been edited by Aphex19: 19 April 2011 - 02:33 PM
#3
Re: Random number probability
Posted 19 April 2011 - 02:19 PM
#4
Re: Random number probability
Posted 19 April 2011 - 02:24 PM
If you know how to construct an SD table, then that's your reference for the game. Say you had 100 numbers, ranging from 0 to 99. How would you create an SD for that set? Easy enough, but what if you had 100 numbers ranging from 0 to 1000?
Think about that, and maybe read the Wikipedia article on SD, and give that a try.
Post up whatever you have for a try, and we'll go from there. Good topic!
This post has been edited by Adak: 20 April 2011 - 07:20 AM
#5
Re: Random number probability
Posted 19 April 2011 - 02:41 PM
Aphex19, on 19 April 2011 - 02:18 PM, said:
Maybe that's not easy to understand, but think of it like this, lets say you roll two dice. How many ways are there to make 1? 0 ways. How many ways are their to make 2? 1 way. how many ways are their to make 3? 2 ways.. etc.. all the way up to 7, where it peaks, then starts decreasing again.
Compile and run this to see what i mean. Sorry, i know it's a crude example.
#include <iostream>
#include <ctime>
int main() {
srand((unsigned)time(0));
int vals[60];
memset(vals, 0, sizeof(int)*60);
for (int i = 0; i < 10000; i++) {
int random_integer1 = (rand()%30);
int random_integer2 = (rand()%30);
int total = random_integer1+random_integer2;
vals[total]++;
}
for (int i = 0; i < 60; i++)
std::cout << vals[i] << " ";
std::cin.ignore();
std::cin.get();
return 0;
}
Sample output
Quote
64 223 317 292 348 288 293 353 329 319 291 288 269 262 243 26
65 173 174 178 131 134 124 100 110 91 90 67 48 44 37 21 10 0
EDIT: Changed the code to generate numbers between 0 and 60, rather than 0 and 12.
I'm not sure if i completely understand your out put that you have. And does that create an equal probability increase/decrease before and after 30? I understand the core of your code, but I also need one number for the "damage", how would I take that from there, Sorry i think im confused.
jimblumberg, on 19 April 2011 - 02:19 PM, said:
I have read a lot about things like that in search of an answer to my question, but I don't have a sophisticated enough knowledge with math to be able to interpret that.
Adak, on 19 April 2011 - 02:24 PM, said:
If you know how to construct an SD table, then that's your reference for the game. Say you had 100 numbers, ranging from 0 to 99. How would you create an SD for that set? Easy enough, but what if you had 100 numbers ranging from 0 to 1000?
Think about that, and maybe read the Wikipedia article on SD, and give that a try.
Post up whatever you have for a try, and we'll go from there. Good topic!
I'm sort of lost.. I don't have the biggest knowledge in math, partially because I'm still a high school student who hasn't finished all their courses in math, but will in the next year.
#6
Re: Random number probability
Posted 19 April 2011 - 02:46 PM
#7
#8
Re: Random number probability
Posted 19 April 2011 - 02:56 PM
Quote
As I understand it, our goal is to generate a random number with a higher probability of being closer to 30, than being closer to 60 or 0, so it is a bell curve, as you explained. Now, let me give a visual example. Let's say we want to generate a random number between 0 and 12, so we roll two dice and add up the result. Look at the following table and see how many ways there are to make each value, given the two dice, each with values 1 to 6 on them.
Quote
1 : No ways to make 1
2 : 1+1
3 : 1+2
4 : 2+2, 3+1
5 : 3+2, 4+1
6 : 1+5, 2+4, 3+3
7 : 6+1, 5+2, 4+3
8 : 6+2, 5+3, 4+4
9 : 6+3, 5+4
10: 6+4, 5+5
11: 6+5
12: 6+6
See how it curves. It is actually misleading, that diagram, because it shows 6,7 and 8 as having the same probability of being thrown, but actually given the different configuration of the die, 7 has the highest. Do this experiment in real life, throw 2 dice, add up the result and figure out which number is more often thrown, it will be 7.
Now, relating the code i posted to the dice example, the array vals reperesents the amount of times each number has been thrown, so each element relates to each total of each dice at each throw. Notice that the middle number is thrown more often.
But aside from technicalities, you can take it for a given that if you take two random numbers from the same range, let's say 0 to 30, and add them up, there will be a greater probability of those two values totalling the middle number (30) rather than the higher or lower number.
This post has been edited by Aphex19: 19 April 2011 - 03:01 PM
#9
#10
Re: Random number probability
Posted 19 April 2011 - 03:44 PM
Aphex19, on 19 April 2011 - 05:03 PM, said:
First of all, if you're not a math major it's not trivial to produce a function "from scratch" that will output random numbers in a normal distribution.
And how "overcomplicated" is it to use the boost library -- where experts have already done the hard part for you? Let's see:
#include <ctime>
#include <iostream>
#include <boost/random.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.hpp>
using namespace std;
int main() {
boost::mt19937 rng;
rng.seed(static_cast<unsigned int>(std::time(NULL)));
boost::normal_distribution<> dist(30.0, 8.0);
boost::variate_generator<boost::mt19937&,
boost::normal_distribution<> > rand_gen(rng, dist);
for( int i = 0; i < 40; ++i ) {
cout << (int)rand_gen() << endl;
}
return 0;
}
That will generate ints with a mean of 30 and standard deviation of 8. If you have a modern compiler, you probably already have Boost random included in it.
Edit: added seed
This post has been edited by r.stiltskin: 19 April 2011 - 03:51 PM
#11
Re: Random number probability
Posted 19 April 2011 - 03:54 PM
r.stiltskin, on 19 April 2011 - 04:44 PM, said:
Aphex19, on 19 April 2011 - 05:03 PM, said:
First of all, if you're not a math major it's not trivial to produce a function "from scratch" that will output random numbers in an normal distribution.
And how "overcomplicated" is it to use the boost library -- where experts have already done the hard part for you? Let's see:
#include <iostream>
#include <boost/random.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.hpp>
using namespace std;
int main() {
boost::mt19937 rng;
boost::normal_distribution<> dist(30.0, 8.0);
boost::variate_generator<boost::mt19937&,
boost::normal_distribution<> > rand_gen(rng, dist);
for( int i = 0; i < 40; ++i ) {
cout << (int)rand_gen() << endl;
}
return 0;
}
That will generate ints with a mean of 30 and standard deviation of 8. If you have a modern compiler, you probably already have Boost random included in it.
You're right, sorry, I withdraw my previous statement. I am actually not a major, nor to I claim to be an expert in mathmatics, but I thought that my solution to the OP's problem was very simple, although I know little of normal distribution, it may well be a much better solution.
No hard feelings.
By the way, how did you know I wasn't a major in maths, I haven't posted anything about my education on my page.. have I? Was it just obvious to you?
This post has been edited by Aphex19: 19 April 2011 - 03:57 PM
#12
Re: Random number probability
Posted 19 April 2011 - 03:59 PM
r.stiltskin, on 19 April 2011 - 03:44 PM, said:
Aphex19, on 19 April 2011 - 05:03 PM, said:
First of all, if you're not a math major it's not trivial to produce a function "from scratch" that will output random numbers in a normal distribution.
And how "overcomplicated" is it to use the boost library -- where experts have already done the hard part for you? Let's see:
#include <ctime>
#include <iostream>
#include <boost/random.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.hpp>
using namespace std;
int main() {
boost::mt19937 rng;
rng.seed(static_cast<unsigned int>(std::time(NULL)));
boost::normal_distribution<> dist(30.0, 8.0);
boost::variate_generator<boost::mt19937&,
boost::normal_distribution<> > rand_gen(rng, dist);
for( int i = 0; i < 40; ++i ) {
cout << (int)rand_gen() << endl;
}
return 0;
}
That will generate ints with a mean of 30 and standard deviation of 8. If you have a modern compiler, you probably already have Boost random included in it.
Edit: added seed
Also the reason i dont use them is because I don't understand them. I like to use code that I understand, otherwise i don't really learn. I dont get how a lot of these things work...
This is what ive done so far unrelating to using boost:
Do you think its accurate? I put a lot of different loops and cout statements to look at what it looks like, and how many times each number is repeated, excuse the many many int variables and loops, just to check stuff...
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
short num1[60];
short num2[60];
short num3[60];
short num4[60];
void main()
{
srand(time(NULL));
for (int x = 1; x <= 60; x++)
{
num1[x] = rand() % 30 + 1;
num2[x] = rand() % 30 + 1;
num3[x] = num1[x] + num2[x];
}
for (int y = 1; y <= 60; y++)
cout << num3[y] << endl;
for (int z = 1; z <= 60; z++)
{
for (int t = 1; t <= 60; t++)
{
if (num3[t] == z)
num4[z] = num4[z] + 1;
}
int f = num4[z];
for (int l = 1; l <= f; l++)
cout << "*";
if (num4[z] != 0)
cout << endl;
}
for (int p = 1; p <= 60; p++)
cout << endl << p << ": " << num4[p] << endl;
}
#13
Re: Random number probability
Posted 19 April 2011 - 04:08 PM
But if you generate two numbers from a uniform distribution of 0-30 and sum them, you actually get a symmetrical triangular distribution of 0-60. I suppose that for some purposes that might be a good enough approximation of a normal distribution but its clearly not the same thing.
#14
Re: Random number probability
Posted 19 April 2011 - 04:11 PM
I am very open to learning how to do it properly, but I'm not at the point where I understand these library editions, and how to work them, partially because I use visual c++ 6.0 and there seem to be many differences in ways things are done.
#15
Re: Random number probability
Posted 19 April 2011 - 04:30 PM
If you want to generate your own set of random numbers, which conform to a bell shaped curve, that is close to a Normal Distribution, first, we need some parameters:
What is the total range of numbers for the set { }, and how many numbers will there be? What value do you want for the mean (average) to be?

New Topic/Question
Reply


MultiQuote



|