473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

A random number generator

Hi all,

I am a newbie with a problem,I want to have a random number generator
which generate numbers satisfied normal distribution(gaussian).I found
that GNU GSL lib provids some functions which could do this ,but i still
can not control the mean value and upper/lower limit it generated.below
is the code :

===================================
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

int main (void)
{
const gsl_rng_type * T;
gsl_rng * r;

// select random number generator
r = gsl_rng_alloc (gsl_rng_mt19937);

double sigma = 10;
for (int i = 0; i < 10; i++)
{
double v = gsl_ran_gaussian_tail(r,4.5, sigma);
}

gsl_rng_free (r);

return 0;
}
===============================

one problem is ,it does not cut off from 4.5 and generated very big
numbers,i want to control the output ,say,between 0-100 with mean 50,is
there any way could do that ?

Thanks a lot,guys!!
Jul 22 '05 #1
1 2081
Joseph wrote:
I am a newbie with a problem,I want to have a random number generator
which generate numbers satisfied normal distribution(gaussian).I found
that GNU GSL lib provids some functions which could do this ,but i still
can not control the mean value and upper/lower limit it generated.below
is the code :


You are asking for an impossibility; numbers which satisfy a gaussian
distribution will not fit within a fixed upper and lower limit. If you
require that the numbers fit within a fixed limit, you no longer have a
gaussian distribution.

I would suggest that you do a little bit of reading on statistical
distributions, find a distribution that fits your requirements, and then
find (or write) some code that implements that distribution. I'm fairly
sure that what you want is a Beta distribution (properly shifted and
scaled); see the url below for details, and do a Google search on "beta
distribution" to figure out how the parameters relate to the mean and
variance.

Meanwhile, from your code:
[...]
double v = gsl_ran_gaussian_tail(r,4.5, sigma);
[...]

There seems to be a reference on this function here:
http://www.lsw.uni-heidelberg.de/man...sl-ref_18.html

It states that this function returns values from the gaussian tail, with
a cut-off such that they will all be greater than 4.5. This seems to
exactly match the problem that you are complaining of.

- Brooks
--
The "bmoses-nospam" address is valid; no unmunging needed.
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.