 |
|
 |
I respect Your solution, but why don't you just use cstdlib rand() function ?
#include
int v = rand() % radius;
gtx
darek
|
|
|
|
 |
|
 |
oops stdlib dissappeared from #include
|
|
|
|
 |
|
 |
Hi Darek -
yip - 100% right= I wrote this back in 2002 and was VERY new to C++ / MFC
|
|
|
|
 |
|
 |
int GetRandomNumber(int a, int b)
{
if(a > b) //doh
a ^= b ^= a ^= b;//XOR trick - See Article by Andreas Saurwein
/////////////////////////////////////////
LARGE_INTEGER freq;
////Add more randomness to the seed
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&freq);
int nNearlyRandom = (int)freq.LowPart;
if(nNearlyRandom < 0)
nNearlyRandom = -nNearlyRandom;
nNearlyRandom + GetTickCount();
////////////////////////////////////////
time_t SeedTime;
struct tm SeedDate;
SeedTime = time(0);
SeedDate = *localtime(&SeedTime);
int FinalSeed = SeedTime + SeedDate.tm_mday + (SeedDate.tm_mon+1) + (SeedDate.tm_year+1900) + nNearlyRandom;
srand((unsigned int) (FinalSeed);
int Interval = b - a + 1;
int RandomOffset = rand() % Interval;
int RandomNumber = a + RandomOffset;
return RandomNumber;
}
"Life begins at 140"
|
|
|
|
 |
|
 |
Ick. Lord no. Lots of problems with this method.
1. Random generator is seeded every time. "rand" sucks bad enough as is, doesn't need to suck worse.
2. QPF isn't portable, the other version is.
3. Seeding the random number every time is not only counter productive, but SLOW....
Tim Smith
"Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution."
Hoare - 1980 ACM Turing Award Lecture
|
|
|
|
 |
|
 |
rand is known to be poor as a random number generator.
Gamers use often the Mersenne Twister algorithm (see this link or this one for more information).
Cryptography uses even stronger generators...
I understand your point is to get a number in a given interval, not how to generate the number.
I just though the information could be of interest in this topic
For printing :
http://www.math.keio.ac.jp/~matumoto/emt.html
http://www-personal.engin.umich.edu/~wagnerr/MersenneTwister.html
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
|
|
|
|
 |
|
 |
Hi Phillippe ,
I agree 100% that rand() is very poor... and that cryptography providers use stronger generators, ---- however they have been attacked / cracked - because of the unreliable method / source of random numbers.
I just used Rand() because I want an a pseudo-random number between and interval, I am not creating a secure algorithm and do not care if someone cracks it
Here is a little something from Bruce Bruce Schneier ...
.."In theory, true random numbers only come from truly random sources: atmospheric noise, radioactive decay, and political press announcements. If a computer generates the number, another computer can reproduce the process. "
Thanks for the input
|
|
|
|
 |
|
 |
Great quote.
Actually, I always wondered why there is no hardware random number generator on motherboards (based on white noise for example).
After all, there are hardware timers...
They would be fast and reliable.
Note that sometime, programmers *want* reproducible sequences, for tests
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
|
|
|
|
 |
|
 |
For small ranges such as 0-1, rand fails to produce random numbers at all. So it really should be avoided.
But that is really a side issue.
Tim Smith
"Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution."
Hoare - 1980 ACM Turing Award Lecture
|
|
|
|
 |
|
 |
I use a method similar to yours for creating a number of die rolls (numbers between 1 and 2,4,6,8,10,12,20,30 and 100).
The only difference is that if only one random number is needed per user click, I use the current time (including the thousandths of the second) to produce the final random number rather than just seed the randomizer.
That way I get more true random numbers instead of just the pseudo-random that the computer can generate.
SunKnight
|
|
|
|
 |
|
 |
So was anyone fired because of your work?
Why not throw away a dime?
I throw away ten pennies all the time.
|
|
|
|
 |
|
 |
No one has been fired .... yet
I now it is cruel for the poor employees to answer riddles all the time - so we replaced the riddles with some TCP/IP subnetting questions
*ok it is cruel, also*
kevnar wrote:
Why not throw away a dime?
I throw away ten pennies all the time.
- A dime is worth 10 cents and is equal to 2 nickels or 10 pennies
|
|
|
|
 |
|
 |
nums wrote:
A dime is worth 10 cents and is equal to 2 nickels or 10 pennies
...yeah?.... I know.....and?
Why not throw away a dime?
I throw away ten pennies all the time.
|
|
|
|
 |
|
 |
ok, here is my answer to your riddle:
kevnar wrote:
Why not throw away a dime?
Why not fire a good employee
kevnar wrote:
I throw away ten pennies all the time.
rather fire 10 bad ones???
If that is not what you want, then I give up!
|
|
|
|
 |