Enjoy an ad free experience by logging in. Not a member yet? Register.

1. well i need an algorithm. thats all. I am using a high-level hardware description language. So eventually the random number generator will be built in hardware, but for now I am simply simulating and so any random number algorithm will do (the language I am using is a variant of C).

i hope that helps.

2. Google "pseudo random generator" and pick the first one.

Dave

3. http://www.agner.org/random/theory/chaosran.pdf

page 4 eqn 16 is straight forward, and easy to implement. The set size
and overall performance will not be great, but it is a good place for you to start.

4. \$spacer_open \$spacer_close
5. Code:
```/* Reentrant random function frm POSIX.1c.
Copyright &#40;C&#41; 1996, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
version 2.1 of the License, or &#40;at your option&#41; any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.  */

#include <stdlib.h>

/* This algorithm is mentioned in the ISO C standard, here extended
for 32 bits.  */
int
rand_r &#40;unsigned int *seed&#41;
&#123;
unsigned int next = *seed;
int result;

next *= 1103515245;
next += 12345;
result = &#40;unsigned int&#41; &#40;next / 65536&#41; % 2048;

next *= 1103515245;
next += 12345;
result <<= 10;
result ^= &#40;unsigned int&#41; &#40;next / 65536&#41; % 1024;

next *= 1103515245;
next += 12345;
result <<= 10;
result ^= &#40;unsigned int&#41; &#40;next / 65536&#41; % 1024;

*seed = next;

return result;
&#125;```

6. awesome. thanks everyone. now i just need to figure out how to get a random first seed.

cheers.

7. I use
Code:
`srand&#40;time&#40;NULL&#41;&#41;;`
as in use the time for the first seed

8. ya...its not that easy with what i am using. I am going to try and see if the development tools I am using include something like a system clock. If not I'll either make one or figure something else out.

cheers

9. I just came across truerand while reading the book "Malicious Cryptography", the truerand algorithm is "truly random" since it relies on the two timers of a motherboard (most have them nowadays) one timer is the CPUclock, and the other the RTC.

In C code this is:

Code:
```#include	<signal.h>

void alarm_handler&#40;&#41;;

int	running	= 1;

int main&#40;&#41;
&#123;
int	i = 0;

signal&#40; SIGALRM, alarm_handler &#41;;

alarm&#40; 1 &#41;;

while&#40; running &#41;
&#123;
i++;
&#125;

printf&#40; "i&#58; %u\n", i &#41;;

exit&#40; 0 &#41;;
&#125;

void alarm_handler&#40;&#41;
&#123;
running = 0;
return;
&#125;```
The alarm is send based on the Real Time Clock, and before the kernel checks the RTC to see if one second has passed, it will run the while loop for an impossible to measurable amount of time(Right? Or does an processor keep track of the instructions it has executed?) , the number of times the while loop is iterated depends on the CPU clock.

10. well thats not truely random ... always. let me explain.

basicly what that code does is see how many lines of code it can go through (or repeat) in a certain amount of time (1 sec did you say? I couldn't get it to run). I have used this function before to check the speed of a computer (in Basic ... don't laugh please). if your variable (i) has enough space so that it doesn't roll over you will notice that the pattern becomes bell/normal shaped not random (provideing simaliar running conditions as in you didn't open more programs etc.) it won't always be that same number because of small factors (human start time to chip sync, heat on the chip, etc)

but when rollover is introduced (which it probably is since your useing int and 1 sec) it because close to (if not completely) random. if you were to change the variable to a bool then I belive that it would (if bools can rollover) be random.

but I would like to see if this is the case, can you post a histogram? (>1000 runs should do)

11. True, true, the results are not spread, but I think you get very close to spreading if you only take the LSB (least significant bits) of the result.

I'll try to get a histogram of both the total results and the LSB of those total results.

--

Boolean values can "roll over", but a boolean is normally stored as a char, so that gives 256 different values, of which only, the 0, is the false value.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•