Random number probability

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 15970 Views - Last Post: 24 April 2011 - 02:10 PM Rate Topic: -----

#1 Kyle_vdk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 19-April 11

Random number probability

Posted 19 April 2011 - 01:55 PM

Hey guys, I'm new here, I haven't been working with c++ for that long, but I have worked with other languages, so I feel like I'm picking this up pretty quickly. I am really stumped on how to do something though, I'll try to explain it:

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

Is This A Good Question/Topic? 1
  • +

Replies To: Random number probability

#2 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Random number probability

Posted 19 April 2011 - 02:18 PM

Maybe if you generate two random numbers, each between 0 to 30, then add them up. It is more probably that you will get a number around 30 because their are more combinations that add to (numbers around) 30 than their are other numbers.

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

12 17 30 46 60 72 84 79 88 107 126 137 173 150 173 173 185 20
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

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Random number probability

Posted 19 April 2011 - 02:19 PM

You may want to look at this link.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Random number probability

Posted 19 April 2011 - 02:24 PM

Interesting question - should be easy enough. Yes, a standard distribution The two centerd area's are within 1 standard of the mean, and include 68% and change, of the total number in the set. The next two area's (one on the left, the other on the right), encompass about 95% of the total distribution. The last two areas include over 99% of the total distribution.

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

Was This Post Helpful? 1
  • +
  • -

#5 Kyle_vdk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 19-April 11

Re: Random number probability

Posted 19 April 2011 - 02:41 PM

View PostAphex19, on 19 April 2011 - 02:18 PM, said:

Maybe if you generate two random numbers, each between 0 to 30, then add them up. It is more probably that you will get a number around 30 because their are more combinations that add to (numbers around) 30 than their are other numbers.

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

12 17 30 46 60 72 84 79 88 107 126 137 173 150 173 173 185 20
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.

View Postjimblumberg, on 19 April 2011 - 02:19 PM, said:

You may want to look at this link.

Jim


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.


View PostAdak, on 19 April 2011 - 02:24 PM, said:

Interesting question - should be easy enough. Yes, a standard distribution The two centerd area's are within 1 standard of the mean, and include 68% and change, of the total number in the set. The next two area's (one on the left, the other on the right), encompass about 99% of the total distribution.

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.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Random number probability

Posted 19 April 2011 - 02:46 PM

I hate to spoil your fun, but why reinvent the wheel?

Boost Normal Distribution
Was This Post Helpful? 0
  • +
  • -

#7 Kyle_vdk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 19-April 11

Re: Random number probability

Posted 19 April 2011 - 02:53 PM

View Postr.stiltskin, on 19 April 2011 - 02:46 PM, said:

I hate to spoil your fun, but why reinvent the wheel?

Boost Normal Distribution

A couple of reasons i guess.. I've never used any.. other libraries and just looking at them they seem to confuse me. a lot
Was This Post Helpful? 0
  • +
  • -

#8 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Random number probability

Posted 19 April 2011 - 02:56 PM

Quote

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.


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

0 : No ways to make 0
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

Was This Post Helpful? 0
  • +
  • -

#9 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Random number probability

Posted 19 April 2011 - 03:03 PM

View Postr.stiltskin, on 19 April 2011 - 03:46 PM, said:

I hate to spoil your fun, but why reinvent the wheel?

Boost Normal Distribution


I actually think that this is overcomplicating it (using libraries such as this), it's a very simple task to solve given the right method.
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Random number probability

Posted 19 April 2011 - 03:44 PM

View PostAphex19, on 19 April 2011 - 05:03 PM, said:

View Postr.stiltskin, on 19 April 2011 - 03:46 PM, said:

I hate to spoil your fun, but why reinvent the wheel?

Boost Normal Distribution


I actually think that this is overcomplicating it (using libraries such as this), it's a very simple task to solve given the right method.


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

Was This Post Helpful? 1
  • +
  • -

#11 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Random number probability

Posted 19 April 2011 - 03:54 PM

View Postr.stiltskin, on 19 April 2011 - 04:44 PM, said:

View PostAphex19, on 19 April 2011 - 05:03 PM, said:

View Postr.stiltskin, on 19 April 2011 - 03:46 PM, said:

I hate to spoil your fun, but why reinvent the wheel?

Boost Normal Distribution


I actually think that this is overcomplicating it (using libraries such as this), it's a very simple task to solve given the right method.


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

Was This Post Helpful? 0
  • +
  • -

#12 Kyle_vdk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 19-April 11

Re: Random number probability

Posted 19 April 2011 - 03:59 PM

View Postr.stiltskin, on 19 April 2011 - 03:44 PM, said:

View PostAphex19, on 19 April 2011 - 05:03 PM, said:

View Postr.stiltskin, on 19 April 2011 - 03:46 PM, said:

I hate to spoil your fun, but why reinvent the wheel?

Boost Normal Distribution


I actually think that this is overcomplicating it (using libraries such as this), it's a very simple task to solve given the right method.


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;	
}



Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Random number probability

Posted 19 April 2011 - 04:08 PM

No hard feelings.

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.
Was This Post Helpful? 0
  • +
  • -

#14 Kyle_vdk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 19-April 11

Re: Random number probability

Posted 19 April 2011 - 04:11 PM

I think it is appropriate for what I am doing.

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.
Was This Post Helpful? 0
  • +
  • -

#15 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Random number probability

Posted 19 April 2011 - 04:30 PM

First, is the above code good enough, or do you want something else?

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?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2