Random numbers between -5.0 to +5.0
Hi,
How to create random numbers between -5.0 to +5.0 ? smp |
-4.2 does that help?
Without a reference to any language or function I presume you just need some thrown around. |
If you have a function that creates (pseudo) random numbers in the range 0..1, a crude way is to simply shift the random number by one half (to "center" the distribution), then multiply by 10. A hopefully clarifying example (let's call the random number generating function rand()):
- initially rand() returns something between 0..1 - (rand() - 0,5) is then something between -0,5..0,5 - 10*(rand() - 0,5) is then something between -5..5 This is not a good way, however, if you need "good" random numbers between -5 and 5 (I'd say if it's a school work, that probably is enough, but if it's not, you'll want to see something else). In addition the quality of the random numbers depends on how you actually pull out the initial randon number; typically language built-in functions for this are not your best option. Better would be if you wrote your own pseudo random number algorithm, but it means extra work and considerations. Note that you'll only get pseudo random numbers, not "real ones". To get good numbers that are more "random" than the typical quick solutions, you'd need to implement some kind of truly random effects into the algorithm, like noise from some physical device. |
Quote:
I am using the same trick as you mentioned. But you said, this is not a good way to create such numbers between -5.0 and +5.0. What is then the better way? smp |
Quote:
For example, there have to be doubts about what the last decimal place will do if you multiply by ten? Is that still going to be 'random' or will it always have some particular value? Of course, there are many applications in which this won't be of concern, because you are going to truncate the string to a fewer number of decimal places. Quote:
|
Well you have basically two choices, either use a better random number generator (Numerical Recipes things are generally fine as educational examples, but hardly "good" when compared to commercial options) to get the random numbers between 0 and 1 that you then "scale", or increase the accuracy of the floating-point numbers and operations. The former one is easier to achieve. You can take a look at mathematical computer science articles (the more recent ones may have fresh ideas), random numbers are the subject of such articles every now and then. Another option is to buy a hardware random number generator, which is usually way better than any pseudo random number generating algorithm.
I'll repeat that if you're being academic here, you can well settle with the Numerical Recipes approach (or other such algorithm). Unless it's security related, you'll probably do well with that. |
Quote:
Using ran1() from NR, I generated these following NRs (3rd column) within -0.5 to +0.5. First column is the direct output of ran1(). 2nd column is subtraction of 0.5 from each entry in 1st column. Then each entry in 2nd column is multiplied by 10. Code:
0.172861 -0.327139 -3.271394 I want to use these NRs in my current task in a Adaptive Optics simulation. I am working as a scientific assistant in a research institute. Quote:
|
Quote:
While I don't know where they come from either, there are a lot of 3s and 4s as terminal digits in that list, and if I thought that might make a difference to what I was ultimately do with the numbers, I'd want to investigate further. Like look at a longer list, and apply a statistical test, rather than just eyeballing the numbers. The 'clumpiness' might not be true of a longer list. Of course, it might not make a real difference to what you do with the numbers; the rest of us cannot really tell from the information supplied, which was a bit of a 'Seattle Helicopter'. |
Quote:
okay. I will check. Thanks. Regards, smp |
depends what they are for, but for testing system robustness to noise i tend to use octave's rand function to generate me a list of psudo random numbers....
if its for cryptography you want some secure hash function of a truly chaiotic input suggest a noisy diode, a geiger counter and a lava lamp somwhere in the process (i belive sun use an array of LDRs pointed at a lava lamp for this) |
Here's a little C program that I think does what you want. It requires "sudo apt-get install libbsd-dev"; you also need to install MPFR (http://www.mpfr.org/).
Code:
/* Generate a random real number x in [0, 1] with uniform distribution */ |
Quote:
Will you please explain this in detail (each word in this command)? I am not expert in linux. regards, |
Linux provides a useful resource for learning answers to questions of this sort. From the command line type:
Quote:
Quote:
I hope this helps! |
All times are GMT -5. The time now is 09:16 AM. |