![]() |
|
#1
|
|||
|
|||
Need help generating a random numberI'm trying to generate a random number between 0.01 and 100 000 but everytime I test my program the same number is generated ,41.0998. Is that suppose to happen?
Here is part of my code: CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: Need help generating a random numberQuote:
Yes, that is supposed to happen the way you wrote your code. The random generator always starts at the same place unless you seed it first. You need to use the srand() function first (and only once). The typical way is to include time.h header and the line CPP / C++ / C Code:
I also note that you want a number between 0.01 and 100 000. rand() only returns 0-32767 so your range of 10 000 000 will be inaccurate. And your computation is off also. First, switch to double. Second, combine 2 randoms to get in the proper range Third, multiply by your lowest value to get the proper magnitude Forth, add your lowest value to prevent 0 __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
|||
|
|||
Re: Need help generating a random numberQuote:
According to the C Standards, the standard library function rand() returns a number in the range 0 to RAND_MAX, where RAND_MAX is an implementation-defined constant which is required to be at least 32767. Implementation-defined means that the programmers who supply the rand() function for a given compiler can make it anything they want (as long as it is at least 32767). Versions of GNU gcc that I am currently using have RAND_MAX = 2147483647. There are two lessons here: 1. Don't make code that only works if RAND_MAX is 32767. 2. Realize that with some C compilers, there are only 32767 possible distinct values that you can get from rand(). This might be bad news, or it might not matter for your application, but it's always something to keep in mind. One way to create random numbers for any range is to convert the integer return value from rand() to a floating point number between 0 and 1 and then multiply by whatever range you want. So, if you want to generate numbers between 0 and 1000000, here is a way: CPP / C++ / C Code:
Here's a run when I used GNU gcc as a compiler: Code:
Here's a run from Borland bcc32 Code:
Subsequent runs gave different count values, but count value was usually pretty close to the expected value. Note that just I can create more-or-less uniformly distributed values between 0 and 1000000 even though there are actually only 32767 possible number values that I can get. For example, I print the ten smallest numbers that I can get from this method: CPP / C++ / C Code:
With BOrland: Code:
So, even though the numbers may be uniformly spaced over the range, I will never see two numbers closer together than roughly 30.5. Maybe that's OK, mabye not. With GNU: Code:
Here the numbers are spaced about .0005 apart. Maybe that's OK; maybe not. It depends. In both cases, the values will be more-or-less uniformly distributed over the range [0, 1000000], but as you can see, the granularity is quite different. For any given application one or both may be OK. For another application, neither may be OK. It's just something to keep in mind. Note, that this says nothing about the quality of the rand() function itself (how "random" are the numbers that you get?) If rand() gives uniformly distributed numbers over its range, you can transform to any range you want, and the numbers are no more or no less "random" than before. Regards, Dave |
|
#4
|
||||
|
||||
Re: Need help generating a random numberQuote:
Thank you for sending me to dictionary Dave, I really needed to find where I placed it To the Original poster (actually, to anyone with interest), here is a good link that shows a very efficient "random" number generator : ISAAC cryptographic random number generator Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#5
|
|||
|
|||
Re: Need help generating a random numberQuote:
Yeah; I was thinking of grains of salt versus grains of flour. Thus the term 'granularity'. Quote:
The one thing that people sometimes ask is, "How random is rand()?" And in my own inscrutable way, I answer their question with a question, "How random to you want it to be?" Different compilers have different ways of implementing the standard library function rand(). The method is often selected for best efficiency on a given architecture, and may or may not be "random" enough for your purposes. (Nothing to do with granularity here, it has to do with the actual statistical probability distribution function of the chosen algorithm.) A given implementation of rand(), might give sequences of individual integers that is very much like what you would expect from a truly random number generator with a uniform probability distribution, but may not be as successful in generating successive pairs of numbers (as you would need for a craps game simulation, for example). That is, even though you could use rand() successfuly to simulate tossing a single die (range 1 to 6) a large number of times, the joint probability distribution for an experiment where you used two successive calls to rand() might not be as good a match for truly random tosses of two dice at a time. That's why you see lots of different "random" number generator programs; you get to pick one that is random enough for your purposes. For important research, the first thing to do is apply enough tests to your "random" number generator to give you a good feeling about its suitability for your application. (By comparing results with known results from problems of the same class as yours.) Regards, Dave "Some people use statistics like a drunk uses a lamp post: for support rather than illumination." ---Mark Twain |
|
#6
|
||||
|
||||
Re: Need help generating a random numberQuote:
From where is that quote taken ? Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#7
|
|||
|
|||
Re: Need help generating a random numberQuote:
Well, here's the thing: I am pretty sure Mark Twain wasn't the first to say it, and I know he wasn't the last. But if I attribute it to "Andrew Lang (1844-1912), Scottish Scholar and Man of Letters" it usually doesn't get a chuckle. (One sports page on the web attributed it to announcer Vin Scully, and I'm sure that old Vin said it too.) In other words, I don't have an exact citation. I have seen this particular quote lots of times here and there, but this part is actually the setup, not the punch line. Here's how the whole thing goes: "Most people use statistics like a drunk uses a lamppost: for support instead of illumination, when in reality they should be used like a dog uses a lamppost, look it over carefully from all angles and then p... on it." Interestingly, in his Autobiography he quotes Benjamin Disraeli, and the following anti-statistics quote is sometimes mis-attributed to Mark Twain. "There are three kinds of lies --- lies, damn lies, and statistics" ---Benjamin Disraeli Regards, Dave |
|
#8
|
||||
|
||||
Re: Need help generating a random numberHey Dave
You know, today I woke up on the left side and now, after reading this post I'm going to work with a smile. Thanks, Kobi. __________________
It's actually a one time thing (it just happens alot). |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| random number generation issues | Jonnyz007 | C++ Forum | 8 | 27-Oct-2005 20:13 |
| Need Help with my Cards Program (C++) | krisopotamus | C++ Forum | 2 | 06-Oct-2005 17:48 |
| Function that returns a random number | Tori | C++ Forum | 4 | 03-Nov-2004 21:48 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 21:25 |
| Random() : Make each number onlu appear once | NiXeN | C++ Forum | 3 | 13-Jan-2004 05:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The