Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Functions and Bell Curves

Status
Not open for further replies.

StevenB

IS-IT--Management
Sep 25, 2000
247
0
0
US
Hi there folks. I'm not a C++ developer, but I thought I'd ask a theoretical question here just out of curiosity's sake.

Does C++ have built-in functions that allow you to generate a random number that would fit a bell curve?

In other words, I want to create a function (or whatever the correct term is) that generates a number from 1-100. If I generate enough of these, the distribution would look like a bell curve. How easy is that to accomplish?

Additionally, how easy would it be to modify the curve thereafter? For example, let's say I wanted to skew the curve so that all numbers would still be between 1-100, but the distribution would be more heavily towards the higher numbers. How easy would that be to accomplish?

Thanks!

Steve
 
Actually, a random number generator will give you a nice flat distribution.

If you want a bell curve, you have to generate multiple random numbers and add them together. The more random numbers, the higher and steeper curve is. The larger the modulus, the wider the distribution along the bottom.

If you want numbers between 1 and 100 in a bell curve, I'd say generated 11 numbers between 0 and 9 and add them together (0-99), then add 1.

I vaguely remember some tricks to skew the distribution high, but no details are coming to me.

I don't remember if the C function is called random() or rand(). I have too many computer languages in my head!

Also, this is a "pseudo random number" generator. That means you'll get the exact same sequence each time you run it. If you want some variety, such as for a game, you need to initialize the random number generator by passing it a "true random" on the first call. I like the time in milliseconds modulus a largish prime number. Again, I'm not 100% sure this is do-able in C. The other option is to take that "true random" number and generate and ignore that many pseudo random numbers so you're starting at a different point in the sequence each time.
 
A sin wave looks pretty close to a bell curve.
You could generate random numbers (with srand() & rand()) between 0 & 1, then pass them to the sin() function...

I'm not that great with trigonometry, but I believe you can multiply a sin to change the width or height, or add to a sin to change its offset.
 
It's a good suggestion. I actually tried googling. The problem is, not being a developer, I didn't understand what a "library" was. :(

What's the super-simplified version of this? A library is something that you can import into your project, so that you have access to use the functions programmed into the library? Is that about it?

Thanks,

Steve
 
There's two parts to your "library": the .lib file itself and the header file (.h).

You include the header file in your C program:
Code:
#include "randomlibrary.h"

You add the library to your link statement.

Now you can call the routines listed in the header file from your program and the compiled versions in the library run at runtime.
 
Thanks everyone for this information. While I doubt I'll be writing any C++ soon, you all helped answer my question about generally how this problem would be approached from a programming perspective!

Thanks!

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top