Is there any function better than rand() that can still get random numbers? If there isn't one, can i make my own in any way?
Is there any function better than rand() that can still get random numbers? If there isn't one, can i make my own in any way?
There are lots and lots
Try: http://www.agner.org/random/ as a start.
Theres this one, for C though (barely any C only code though).
http://thorkildsen.no/faqsys/docs/random.c //Labelled the "best" method, but it was written a while ago so who knows.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
I dont know if it's the best method. But I do know the author is a void mainer.Originally Posted by Dae
First you have to define what you mean by 'good' so you can then evaluate 'better'. What are you going to use the random numbers for? The criteria for cryptography is much different than that for long-running simulations.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
In 99% of situations, rand() is good-enough if you seed it with time. All you need is a number that's unpredictiable. rand() is unpredictible enough for most games and simulations. You would generally need a large number of samples before you start to see a pattern or a trend. ...If you're gambling, you'll run out of money long before you figure-out how to out-smart rand()!
Before you try to improve-on rand() you should determine what it is about rand() that needs improving. And, your version should not have weaknesses or limitations that are not present in rand().
Just for fun... from the C language standard: *
*The C++ standard simply refers-back to the C standard.7.20.2 Pseudo-random sequence generation functions
7.20.2.1 The rand function
Synopsis
1DescriptionCode:#include <stdlib.h> int rand(void);
2 The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.
3 The implementation shall behave as if no library function calls the rand function.
Returns
4 The rand function returns a pseudo-random integer.
Environmental limits
5 The value of the RAND_MAX macro shall be at least 32767.
7.20.2.2 The srand function
Synopsis
1DescriptionCode:#include <stdlib.h> void srand(unsigned int seed);
2 The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.
3 The implementation shall behave as if no library function calls the srand function.
Returns
4 The srand function returns no value.
5 EXAMPLE The following functions define a portable implementation of rand and srand.
Code:static unsigned long int next = 1; int rand(void) // RAND_MAX assumed to be 32767 { next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768; } void srand(unsigned int seed) { next = seed; }
Not to be the one to toot another member's horn, but this is a good read:
http://eternallyconfuzzled.com/articles/rand.html
and here is their rand library:
http://eternallyconfuzzled.com/libs/jsw_rand.html
Sent from my iPadŽ
MTwister is fast and has a period of 2^19937-1
http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html
I work in the encryption department for NORAD as a NATO liason, and I use this random number generator:Is there any function better than rand() that can still get random numbers?
gdubya likes to keep it simple.Code:cout<<"Enter a number: "; int randNum=0; cin>>randNum; short presidentsCode = encryptTheLaunchCodes(randNum);
Last edited by 7stud; 03-08-2006 at 10:09 PM.
smartass
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
7stud does have a point though. One could go and do something like the HotBits setup.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I've seen people discuss using static (as in using a radio tuned to nothing) to generate randomness...
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
just an idea...
can i use an unallocated memory, or a random memory address, cast what3v3r values it has as an integer and use it as a random number. provided that it will not change it in anyway just read and maybe copy to a variable?
crazy idea... (please don't flame me...)
TIA.
Last edited by what3v3r; 03-09-2006 at 03:42 AM.
> can i use an unallocated memory, or a random memory address
No.
Even just reading random addresses which you don't own can cause things like segmentation faults.
Originally Posted by what3v3r
Memory contents are very poor sources of randomness. In particular, most OSs will zero memory before providing it to a process, which makes the memory non-random in the first place. Second, it is a very rare data set that randomly distributed data and binaries are far from random. However, if you want to run a hash algorithm (like SHA-256) on a chunk of memory then you might be able to use that as the initiation to a pseudo random number generator. You would have to build the functionality into the Kernel as only the Kernel has full access to memory.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw