Sponsored by: █ Sparkhost - Hosting Without Compromises! █ Hybrid Performance Web Hosting █ Spark Host Stream Hosting █ Hybrid IRC & IRCd Server Shell Accounts
Generating Random Number
Posted 12 September 2007 - 11:36 PM
in C language: i use "srand" and "rand" functions.
what do i do?
Posted 13 September 2007 - 05:42 AM
7.1.9 Random Number Generation: Random, Randomize
The Standard Library Random routine generates a sequence of pseudo-random numbers. It returns a random value in the ax register on each call. You can treat this value as a signed or unsigned value since Random manipulates all 16 bits of the ax register.
You can use the div and idiv instructions to force the output of random to a specific range. Just divide the value random returns by some number n and the remainder of this division will be a value in the range 0..n-1. For example, to compute a random number in the range 1..10, you could use code like the following:
random ;Get a random number in range 0..65535.
sub dx, dx ;Zero extend to 16 bits.
mov bx, 10 ;Want value in the range 1..10.
div bx ;Remainder goes to dx!
inc dx ;Convert 0..9 to 1..10.
; At this point, a random number in the range 1..10 is in the dx register.
The random routine always returns the same sequence of values when a program loads from disk and executes. Random uses an internal table of seed values that it stores as part of its code. Since these values are fixed, and always load into memory with the program, the algorithm that random uses will always produce the same sequence of values when a program containing it loads from the disk and begins running. This might not seem very "random" but, in fact, this is a nice feature since it is very difficult to test a program that uses truly random values. If a random number generator always produces the same sequence of numbers, any tests you run on that program will be repeatable.
Unfortunately, there are many examples of programs that you may want to write (e.g., games) where having repeatable results is not acceptable. For these applications you can call the randomize routine. Randomize uses the current value of the time of day clock to generate a nearly random starting sequence. So if you need a (nearly) unique sequence of random numbers each time your program begins execution, call the randomize routine once before ever calling the random routine. Note that there is little benefit to calling the randomize routine more than once in your program. Once random establishes a random starting point, further calls to randomize will not improve the quality (randomness) of the numbers it generates.
Posted 13 September 2007 - 01:27 PM
get the stack beginning/end from the TIB, then read in 4 bytes from some chosen places on the stack (i'd first check against 0, because most of the stack is 0..) and build a dword value like that. this will be 100% random and you can do it without api calls
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users