Allegro.cc Forums » Programming Questions » [A5] Random numbers

 This thread is locked; no one can reply to it. 1   2
 [A5] Random numbers
 Dork270 Member #12,583 February 2011 Using Allegro 5, is there an effective way to generate a random number besides using "srand(time(NULL)?"
 Matthew Leverton Supreme Loser January 1999 Sorry I took so long to reply, but I just got back from reading the manual. There are no random number generating functions in it.By the way, srand() is how you seed the RNG. rand() is how you generate a random number.
 type568 Member #8,381 March 2007 There are various third party libraries for random number generation if you dislike the flawed rand() & require something truly random.www.google.com
 Neil Roy Member #2,229 April 2002 I have some code I got from somewhere many years ago. It's very simple code and obviously not going to be "truly random" (I don't think anything can be to be honest). But it's handy if you want your code to be portable. Give then same seed it will generate the same sequence of numbers, which is what I originally wanted.```unsigned long rng_seed; unsigned long rng (void) { return ((rng_seed = (rng_seed+1)*314159265L) >> 16) & 0xFFFF; } // use it something like this rng_seed = (unsigned)time(NULL); for (i=0; i<100; i++) printf("%d\n", rng()%100); ``` As I said, probably the simplest of random functions you'll ever see, but it works and it's portable. ---“I love you too.” - last words of Wanda Roy
 type568 Member #8,381 March 2007 Neil Roy said: It's very simple code and obviously not going to be "truly random" (I don't think anything can be to be honest). Well of course, but rand() is not serious.. It has very visible patterns, in certain cases bit too predictable.Append:Seems like nice code, could be used quite well in some short-on-libraries place
 Arthur Kalliokoski Second in Command February 2005 You could always try the Mersenne Twister or rip the drand48 functions from djgpp if your libs don't have it. They all watch too much MSNBC... they get ideas.
 Tobias Dammers Member #2,604 August 2002 type568 said: There are various third party libraries for random number generation if you dislike the flawed rand() & require something truly random. If you want truly random, you'll need a physical source of entropy: A sample of a radioactive substance would be perfect, but if you don't have any fissionable materials at hand, you can pick up pretty good noise from a radio (make sure you don't accidentally hit any actual signal though), or you could use a diode and make it 'leak' (that is, throw a high enough voltage at it to make it conductive in the 'wrong' direction; the conductivity will go on and off at fairly random intervals), etc.For all practical purposes (except encryption) however, Mersenne Twister is good enough, and, most importantly, it's fast. The wikipedia article Arthur linked to has links to excellent implementations in C and C++ IIRC. ---Me make music: Triofobie---"We need Tobias and his awesome trombone, too." - Johan Halmén
 Neil Roy Member #2,229 April 2002 Tobias Dammers said:A sample of a radioactive substance would be perfect ---“I love you too.” - last words of Wanda Roy
 type568 Member #8,381 March 2007 Fissile material sounds good.. /me went to buy few kg Pu-238 in the local flee marketBut overall, as of random.. That is very much dependent on the degree of "randomness" required.. For brutal testing purposes, without care about performance various irrational numbers calculation & processing can be doing quite good I suppose.Anyways.. Function that easily gives 15 even numbers in a row, but never gives 16 even numbers in a row is a poor randomizer. (that's the behavior of MSVS rand() )
 Johan Halmén Member #1,550 September 2001 I guess it would be very simple to design a small device, that wouldn't cost more than a few bucks, and you could place it inside your computer. Or connect to a USB port. And it could be supported by the system, or by some driver. It could receive white noise from the UHF band. Or it could have same radioactive stuff that common fire detectors contain. One could read say 64 random bits every nth millisecond. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.
 type568 Member #8,381 March 2007 I'm not sure software solution with access to the mouse wouldn't handle..
 gnolam Member #2,030 March 2002 Pu-238 isn't fissile, and there's no need to use a fissionable isotope for a random number generator either. -- Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!
 tobing Member #5,213 November 2004 Matthew Leverton said: Sorry I took so long to reply, but I just got back from reading the manual. There are no random number generating functions in it. Wow. So either you read fast, or the manual is rather short...
 type568 Member #8,381 March 2007 @molangCrap. And I was wondering why was it so cheap./me has gone to find someone could enhance my Pu with an extra electron..
 Elias Member #358 May 2000 Neil Roy said:As I said, probably the simplest of random functions you'll ever see, but it works and it's portable. This has come up in several threads here in the past, but the "it works" is questionable Run for example this code: #SelectExpand 1#include 2#include 3#include 4#include 5 6static unsigned long rng_seed; 7static unsigned long rng (void) { 8 return ((rng_seed = (rng_seed+1)*314159265L) >> 16) & 0xFFFF; 9} 10 11int main() { 12 rng_seed = time(NULL); 13 14 FILE *f = fopen("rand.pbm", "wb"); 15 fprintf(f, "P1\n"); 16 fprintf(f, "256 256\n"); 17 18 for (int y = 0; y < 256; y++) { 19 for (int x = 0; x < 256; x++) { 20 int r = rng() & 1; 21 fprintf(f, " %d", r); 22 } 23 fprintf(f, "\n"); 24 } 25 26 fclose(f); 27 return 0; 28} It outputs a rand.pbm which looks like this: http://allefant.com/allegro/rand.pngIt's certainly a nice pattern, but not what you would normally call random.The mersenne twister mentioned above is slightly more complex code (but not much) but you will have a much harder time coming up with a test case showing how it's not random In addition to not being random, the rng() function above also has only 65536 different seeds. So if you generate e.g. a random map with 1000x1000 tiles, out of the 2^1000000 possible worlds you are limited to 2^16 worlds. With MT you could get 2^20000 or so different worlds. --"Either help out or stop whining" - Evert
 gnolam Member #2,030 March 2002 type568 said:/me has gone to find someone could enhance my Pu with an extra electron.. You want it ionized? Anyway. The Mersenne Twister is pretty much the standard "good" PRNG, and the reference implementation is BSD-licensed. -- Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!
 Audric Member #907 January 2001 One other case where a good RNG is needed is any game with random "drops" in a huge catalogue of items, each one with a "drop rate". You'll really expect the item with 3/10000th to be less likely than the one with 5/10000th, and at this scale, bad RNGs will cause problems.
 type568 Member #8,381 March 2007 I want it fissile so that I can has TRUE randomness!
 Timorg Member #2,028 March 2002 A decent operating system will have the /dev/random file, that you can read random numbers that are generated by the system running. It could be a useful source of randomness.Sorry for derailing your derailing. ____________________________________________________________________________________________"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophileOMG my sides are hurting from laughing so hard... :D
 type568 Member #8,381 March 2007 I thought it could be OS duty to provide the user with random bits.
 Audric Member #907 January 2001 Except in the situations where your program needs reproducible series : for demo-replaying, or in order to keep synchronized 2 instances of program over network.
 type568 Member #8,381 March 2007 It's different story.
 Vanneto Member #8,643 May 2007 The same seed will always produce the same sequence of random numbers AFAIK. So its just a matter of remembering the seed you used and passing it to the RNG. In capitalist America bank robs you.
 SiegeLord Member #7,827 October 2006 Vanneto said: The same seed will always produce the same sequence of random numbers AFAIK. So its just a matter of remembering the seed you used and passing it to the PRNG. RNGs have no seeds. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]
 AMCerasoli Member #11,955 May 2010 If you know that something is random, then wouldn't be random...
 1   2  Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads