![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
A float random number in a given range, different everytime function is calledI am working on a program which will need several different integer and float random numbers at different stages, for example:
- At one point, I need a random number (float) in the range 0.1 to 10.0 - At one point, I need a random number (float) in the range 0.5 to 1.5 - At one point, I need a random number (float) in the range 0.3 to 3.0 - At one point, I need a random number (float) in the range 0.1 to 10.0 Also, I need to make it sure that it generates different random numbers everytime each of these functions is called in the program. For the integer random numbers, rand () is generating the same random number everytime, so I tool help from planet source code's algorithm which is as follows: CPP / C++ / C Code:
========================= when i optimized the code to generate floating random numbers from 0.1 to 10.0 range, it was generating like 1.1, 2.1, 6.1 etc which is not purely random. Last edited by LuciWiz : 03-Jun-2009 at 02:20.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|||
|
#2
|
||||
|
||||
Re: A float random number in a given range, different everytime function is calledYou need to srand() once at the beginning of the program.
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#3
|
|||
|
|||
Re: A float random number in a given range, different everytime function is calledQuote:
See Footnote. It may be usable for your application if you do something like. CPP / C++ / C Code:
Here are a couple of runs: Code:
Code:
Quote:
What the heck do you mean by "purely random?" Tell us exactly what you would expect to see from three values returned by the function. Then consider this: If it generates exactly what you expect, then it is not very random is it? As a matter of fact, I would call that as un-random as you could possibly get. Let's do a little "thought experiment." Suppose I shuffle a deck of cards. Now, if you don't know what to expect, then you think it's random. OK? Now let's suppose that I know exactly what to expect. OK? I'll open for 20 dollars. Bottom line: There are lots of tests to determine the "randomness" of an algorithm, but only you can determine whether it is "random enough." Regards, Dave Footnote: Since the code that you posted mentions Knuth, volume II, I will give you a quote from that book: Quote:
Last edited by davekw7x : 03-Jun-2009 at 12:02.
|
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 65 | 19-Aug-2009 08:50 |
| Problem executing nam-1.13 | RodolfoAlvizu | Computer Software Forum - Linux | 20 | 28-Feb-2009 16:23 |
| Would Anyone Like to do This? | bradc0101 | C Programming Language | 0 | 30-Oct-2006 20:57 |
| Converting a number amount to text | Godzilla | C++ Forum | 5 | 31-Mar-2006 12:38 |
| Can somebody look at this and point out any errors to me | soulfly | C Programming Language | 7 | 31-Mar-2004 10:45 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The