Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sat Oct 03, 2020 8:06 am

Tobias Claren wrote:
Fri Oct 02, 2020 8:20 pm
@ Heater
I need a true random number generator for applications like the ones carried out by Princeton University in "PEAR". Micro psychokinesis experiments.
Therefore I need for example this "50/50" representation.
Only the distribution is important.
Such a "true random number generator" is typically called a "noise source" in the engineering world. Think about the "fizz" you hear coming out of headphones or the speckles one sees in photographs taken in poor light. Or infamous noise of the Cosmic Microwave Background radiation:
https://en.wikipedia.org/wiki/Cosmic_mi ... background

If you really want such a noise generator you can make one really easily with just two transistors. As shown here: https://web.jfet.org/hw-rng.html

Image

I have actually built that circuit. I works very well. I was compelled to do so because it seemed such a fascinating arrangement of transistors I had to see it working for myself.

There some problems with it though:

1) It is greatly effected by power supply voltage changes and unwanted signals on the supply, by temperature, by any electrical interference, magnetic fields, etc. These external influences must be removed so as to only show up variations due to the psycho waves you are looking for, they are surely much bigger effects than that.

To minimize that you would have to take the precautions mentioned in the link:
encase the thing in a copper box surrounded by a mu-metal box, power it internally with two 9-volt batteries, and use the last inverter to drive an
LED that shines thru a hole in the wall of the box to an external phototransistor (better use a real fast phototransistor)
Of course all that shielding likely also blocks the psycho waves you are wanting to detect, thus ruining the experiment.

2) If you were to take a huge sample of the sequence of zeros and ones coming out of such a circuit directly and subject them to statistical tests of randomness you would find they fail. See: https://en.wikipedia.org/wiki/Randomness_tests

Most importantly there will not be a 50/50 balance of zeros and ones. There will be a bias.
Not only that there will be a frequency spectrum that is not flat in it's output. See "white noise", "pink noise" etc:https://en.wikipedia.org/wiki/Colors_of_noise

Arguably one could do the psycho wave experiments with that bias. Just observe how it changes over time. But you are going to have a really hard time measuring that bias, and other statistical problems, accurately. It will take a huge a lot of time and hours of statistical analysis with the programs available. Such bias and other statistical problems will vary over time anyway. Thus making it very hard to separate any psycho wave effects from natural variations. The experiment won't work.

3) Typically people who want a source of random numbers take such noise and do some processing on it. In digital logic hardware or software. Likely starting with a simple and fast circuit to remove the bias of zeros and ones and get an equal number of both. Like the Von Neumann method. See Von Neumann, famous paper here:
https://dornsifecms.usc.edu/assets/site ... p36-38.pdf

Of course when you start adding such hardware and software post-processing to achieve a 50/50 bias and fix other statistical issues you are no longer using the "true" randomness of your original noise source. Such post-processing therefore ruins the psycho wave experiment.

4) We could replace such a simple electronic circuit with something very much not affected by local PSU variation, temperature variation, magnetic fields etc. Say something quantum mechanical. Like listening the the "ticks" of radiation coming out of a radio active source.

But, if those psycho waves are so weak as be hard to detect by moving macro sized matter or electrons in a noise generator like the above I am very much inclined to think they are far to weak to affect what goes on in the nuclei of atoms.

All in all, I see no sensible way to make such psycho wave experiments in such a way that the could prove or disprove anything.

I urge you to build that noise source. It's fascinating. Just put an amplifier on it's output. Listen to it. Hear how it changes with supply voltage.

Then perhaps you will start to understand what you are up against with these psycho wave experiment idea.
Memory in C++ is a leaky abstraction .

pidd
Posts: 1296
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: Random Number Generator Using Build In Hardware

Sat Oct 03, 2020 3:11 pm

Heater wrote:
Sat Oct 03, 2020 2:23 am
pidd wrote:
Fri Oct 02, 2020 8:43 pm
There is no such thing as a true random number generator, all hardware is influenced by something, be it gravity, magnetism, light, radiation, movement, pressure etc etc. That is why the best (least worst) random number generators we have are algorithms seeded and re-seeded by hardware noise generators, the combination reduces the influence of the inadequacies of each individual method.
Thing is, a source of random numbers is not actually what these guys want. What they want is a way to measure and demonstrate the effect of "psychokinetic waves" or force or whatever it is on actual matter as we know it.
If the source can be influenced by the same things as the "influencer" can be influenced by then the measurement is meaningless.

On a theoretical platform, I agree that influencing is possible, in theory it is possible to read someone's brainwaves from 50,000 miles away. Every particle or force never mind how small has an impact on every other particle or force in the universe.

On a reality platform, the influence is minute to the point of being insignificant, trying to measure it at a significance level is currently impossible.

ejolson
Posts: 6028
Joined: Tue Mar 18, 2014 11:47 am

Re: Random Number Generator Using Build In Hardware

Sat Oct 03, 2020 4:37 pm

pidd wrote:
Sat Oct 03, 2020 3:11 pm
Heater wrote:
Sat Oct 03, 2020 2:23 am
Thing is, a source of random numbers is not actually what these guys want. What they want is a way to measure and demonstrate the effect of "psychokinetic waves" or force or whatever it is on actual matter as we know it.
If the source can be influenced by the same things as the "influencer" can be influenced by then the measurement is meaningless.
If a thermal source of random numbers is, for example, biased depending on the ambient temperatures, wouldn't the obvious way for a novice to influence the outcome be using the Force to decrease the temperature of the room?

In particular, if aligning the air molecules of the room in a way that avoids collisions turns out to be too difficult (or causes difficulty breathing), then the emergent psychokinetic powers should be focused on the hysteresis loop in the thermostat. This could be a statistically significant demonstration of talent for thermostats controlled by a Raspberry Pi using fuzzy logic derived from the hardware random number generator.

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sat Oct 03, 2020 6:16 pm

What are the para-* guys looking for? And why?

I'm with pidd there. I assume my mind, my consciousness, my being as I know it, all happens in that blob of gloop in my head, my brain.

For sure there are things going on in my blob of gloop. Involving chemical reactions, neurons exchanging signals, electrically and chemically, all kind of chaos.

I'm pretty sure that chaos in my mind can have effects outside. After all, if even one electron moves in one synapse of by brain, it wiggles an electric field, generates a magnetic field, even disturbs gravity, in some small way, that radiates out to the rest of the universe at the speed of light.

We even have MRI machines now a days that can see something of that activity going on inside brains. How amazing is that?

But then, some of that noise going on in my head results in me typing messages into forums like this. Which for sure have had remote physical effects on macro sized objects in the world, thousands of miles away. How frikken amazing is that?

In short, humans communicate with humans over huge distances, and even through time, and that has big physical effects. Never mind the internet, we have been doing this via sound, print, telegrams, radio, TV, for ages.

All of this I find mind blowing. Despite the fact the the mechanisms by which it works are pretty well understood physical phenomena.

I always wondered how it is possible that the para-* guys are not totally blown away by all this, not curious how it all works, and still yearn for for some spooky "other thing".
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1028
Joined: Mon Jun 11, 2018 11:22 am

Re: Random Number Generator Using Build In Hardware

Sat Oct 03, 2020 6:30 pm

for real random number generating, I would suggest to simply hooking the logic circuits of a Bambleweeny 57 Sub-Meson Brain to an atomic vector plotter suspended in a strong Brownian Motion producer (say a nice hot cup of tea).

Image

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sat Oct 03, 2020 6:57 pm

dsyleixa123 wrote:
Sat Oct 03, 2020 6:30 pm
42
Brilliant!

Douglas Adams, still having real, measurable, physical effects on the world.

Many decades after he first though that.

And, sadly, from the grave.

The para-* guys can only dream of having such power!

:)
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1028
Joined: Mon Jun 11, 2018 11:22 am

Re: Random Number Generator Using Build In Hardware

Sat Oct 03, 2020 7:29 pm

Viewed from the standpoint of the spacetime continuum theory (general theory of relativity), all times in the past and future are constantly present at every point of spacetime in the Universe. Just like on a movie DVD that I'm watching now, but where the very beginning and the ultimate end are already unalterably present.
This, in some respect, limits the effects which random numbers, either if pseudo or real, (or even Douglas Adams) might have.
Nonetheless, still, chapeau! before the foresight and ingenuity and humor of this author! :mrgreen:

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 7:46 am

dsyleixa123 wrote:
Sat Oct 03, 2020 7:29 pm
This, in some respect, limits the effects which random numbers, either if pseudo or real, (or even Douglas Adams) might have.
As far as I understand it is not the claim of the psychokinesis supporters that random numbers effect anything.

Rather it is the other way round. The idea of psychokinesis is that the power of a mind can have an effect on external material by some means other than those understood by contemporary science. That is to say not by regular mechanical, electrical, magnetic etc forces. As wikipedia says "... psychic ability allowing a person to influence a physical system without physical interaction".

Which is an interesting way to put it, it presupposes that ones psyche is something other than the physical stuff we know of that your brain/body is made from. Which is already an unsupported claim, or at least an open question. But never mind.

Also, as far as I understand, all experiments to demonstrate such an effect have had a null result. Or all such demonstrations have turned out to be fakes and charlatans. See Uri Geller for example.

Enter the idea of the mind effecting random noise generators. The idea being that perhaps these psychic waves are so week that we cannot detect them effecting macro sized material, but perhaps the effect they have on tiny things, like the movement of atoms and electrons, can be somehow amplified and made detectable by observing the random noise coming out of systems and statistically analyzing it.

Personally I think this doomed to failure. As I said above, effectively what they do in such experiments is to throw away the signal they are looking for and keep the noise.

Now that one has a stream of random numbers with all kind of statistical fluctuations going on, as random processes do, one can correlate that with almost anything else going on, including ones own thoughts, and claim "look, I thought this and the numbers changed like that, see it works!"

Let's just say, I am very doubtful about the ideas here and even more doubtful of the sense of such experiments.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1028
Joined: Mon Jun 11, 2018 11:22 am

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 8:50 am

I probably have missed the psychokinesis topic, but many years ago I was experimenting with Monte-Carlo-Filters for robot positioning, which require "very good" PRNGs.
Linear congruence generators like by Kernighan & Ritchie failed, but the Mersenne Twister even in the TT800 version worked like a charm - it was seeded by random seeds computated from actual time (millis) and the ADC value of an unused, unconnected ADC pin.
At that time I had no built-in PRNG available, certainly not even a validated and certificated thing like that from Intel or whatever.
So from my experience I actually doubt that sth like "a real" RNG (like based on "strong Brownian Motion producers" ;) ) would be needed for scientific purposes, instead a "golden standard" PRNG would always fit.
Other way round, as to experiments for influencing "natural" RNG sequences, combined with randomness tests, your electronic circuit looks really amazing.
Image

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 10:27 am

dsyleixa123 wrote:
Sun Oct 04, 2020 8:50 am
Linear congruence generators like by Kernighan & Ritchie failed, but the Mersenne Twister even in the TT800 version worked like a charm
Yes indeed. There are many better PRNG's out there now a days.

To my taste the Mersenne Twister is a bit over complicated, big and slow given that it is not cryptographically secure. Particularly for use in micro-controllers.

I like the xoshiro / xoroshiro generators. Very small, very fast, very good statistical similarity to random. I know guys who have spent countless compute hours trying them with all kind of tests of randomness they came out very well.
http://prng.di.unimi.it/
Lot's of source code of them there to play with.

I also like the PCG PRNGS. Which by all accounts are even better if a bit slower.
https://www.pcg-random.org/index.html
https://www.pcg-random.org/download.html

There is a great presentation about random numbers and PRNG by the creator of PCG here:
"Stanford Seminar - PCG: A Family of Better Random Number Generators" - https://www.youtube.com/watch?v=45Oet5qjlms
Memory in C++ is a leaky abstraction .

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 10:41 am

Tobias Claren wrote:
Wed Sep 30, 2020 9:08 pm
Does a Raspberry PI contain a real random number generator?
...
Like this USB stick, which is used at Princeton University for parapsychological experiments (micropsychokinesis):
https://ubld.it/truerng_v3
E.g. in "The Global Consciousness Project".
I had to check that USB truerng link. There it says:
The new entropy mixing algorithm takes in 20 bits of entropy and outputs 8 bit to ensure that maximum entropy is maintained. The algorithm uses multiplication in a galois field similar to a cyclic redundancy check to mix the ADC inputs thoroughly while spreading the entropy evenly across all bits.
Well, that means that whatever biases, statistical fluctuations, etc, the actual noise source may have they are all washed away by the "algorithm" in there.

That means that whatever effect on the noise source by pycho-waves there may be will also be washed away. Making any experiment that relies on true randomness using that device null and void.

This is what ejolson was pointing out in a previous post here.

I'm now convinced.

Those Princeton University guys have no idea what they are doing and are totally misguided.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1028
Joined: Mon Jun 11, 2018 11:22 am

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 10:52 am

as to
http://prng.di.unimi.it/xoroshiro1024plusplus.c
how would this prng be #included, seeded and called, similar to
srand(seed) and rand()?

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 11:10 am

Further to the truerng_v3 link in notice that at the link above it says:
The TrueRNG Hardware Random Number Generator uses the avalanche effect in a semiconductor junction to generate true random numbers. The avalanche effect has long been used for generation of random number / noise and is a time-tested and proven random noise source.


So it is actually using the same two transistor noise source as the circuit I presented above.
Memory in C++ is a leaky abstraction .

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 11:23 am

dsyleixa123 wrote:
Sun Oct 04, 2020 10:52 am
as to
http://prng.di.unimi.it/xoroshiro1024plusplus.c
how would this prng be #included, seeded and called, similar to
srand(seed) and rand()?
Looks like that reference code is one of those #include the .c file jobbies we were discussing elsewhere. So just

Code: Select all

#include "xoroshiro1024plusplus.c"
And away you go.

As for seeding the thing it tells you in the comments in the file:
The state must be seeded so that it is not everywhere zero. If you have
a 64-bit seed, we suggest to seed a splitmix64 generator and use its
output to fill s.

The state in the 'static uint64_t s[16];' array. You just have to fill that with your random seed. Which should never be all zeros.

Then you can just call "next(void)" to get successive pseudo random numbers out of the sequence:

Code: Select all

    next_random_number = next();
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1028
Joined: Mon Jun 11, 2018 11:22 am

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 11:30 am

what if I had for seeding just uint32_t millis()? how to proceed then to fill that s[16] array?

User avatar
jahboater
Posts: 6299
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 11:57 am

dsyleixa123 wrote:
Sun Oct 04, 2020 11:30 am
what if I had for seeding just uint32_t millis()? how to proceed then to fill that s[16] array?
Anything you like:-
1) leave the remainder as zero (have to check millis() didn't return zero)
2) use an LCG to compute them from the first millis() value (I think MT1997 does something like to seed its 620 int state)
3) use the suggested splitmix64 whatever that it is
4) use /dev/urandom (just read 16 bytes from it)
5) use the RDSEED or RDRAND instructions on Intel
6) use clock_gettime() (giving nano second resolution)
7) etc etc

As Heater says, anything as long as its not all zero.
The seed is just a "state" and all state values are valid except zero.
Pi4 8GB running PIOS64 Lite

dsyleixa123
Posts: 1028
Joined: Mon Jun 11, 2018 11:22 am

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 12:11 pm

sorry, I don't understand this uint64_t s[16] seed thing (nor the splitmix64 thing) ;
will I have to call millis() 16 times one after another (perhaps by random delay() in between) to fill the array s[0]...s[15]?

User avatar
jahboater
Posts: 6299
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 12:31 pm

dsyleixa123 wrote:
Sun Oct 04, 2020 12:11 pm
sorry, I don't understand this uint64_t s[16] seed thing (nor the splitmix64 thing) ;
will I have to call millis() 16 times one after another (perhaps by random delay() in between) to fill the array s[0]...s[15]?
That's not very random, though it would probably do.
I would call millis() once and use a decent 64-bit LCG to fill in the rest (I seem to remember that's what the Mersenne Twister does).

Code: Select all

uint64_t s[16];

s[0] = millis();  // say 

for( int i = 1; i < 16; ++i )
  s[i] = UINT64_C(6364136223846793005) * s[i-1] + UINT64_C(1442695040888963407);
that should do it.

The LCG here is a well known full period 64-bit one from Donald Knuth.
Another proven 64-bit LCG from L'Ecuyer is slightly smaller and has a prime addend.
state = UINT64_C(2862933555777941757) * state + UINT64_C(3037000493)

But as stated above, any non-zero values should be fine.
An advantage of LCG's are that the addend ensures a zero state is recoverable from.
Last edited by jahboater on Sun Oct 04, 2020 12:39 pm, edited 1 time in total.
Pi4 8GB running PIOS64 Lite

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 12:38 pm

dsyleixa123 wrote:
Sun Oct 04, 2020 12:11 pm
sorry, I don't understand this uint64_t s[16] seed thing (nor the splitmix64 thing) ;
will I have to call millis() 16 times one after another (perhaps by random delay() in between) to fill the array s[0]...s[15]?
Every pseudo random number generator has a "state". A single number or a bunch of numbers that it will scramble up to generate the next random number output value.

In this case the "state" is not a single 64 bit integer but rather an array of 64 bit unsigned integers with 16 elements.

They have called it "s" in this code. Why they could not call it "state" is beyond me.

To get this thing started you need to fill those 16 elements of the array with nice random looking bits. Either a fixed seed if you want to repeat the sequence some time, or some random values you got from somewhere. Be sure they are not all zero.

If you want a different sequence every time you run your program calling millis is not a good way to do it. Why? Because who says you won't get all zeros? Unlikely but not sure. Calling millis many times is not good either. While we might expect the first value to be random enough the subsequent values are not, they come in a rising sequence after all.

Putting a random delay between those calls to millis is also not a good idea. It will take extra time for no useful purpose. Why not just use the random numbers you are going to use for the delay to make the seed values directly?

Typically those that want a known seed so as to be able to repeat a sequence at will only want a 32 or 64 bit seed value. But we need 16 values here. What to do?

Well, why not use some other, smaller, simpler, not so good, PRNG to generate a sequence of 16 unsigned integers that are guaranteed not to be all zero, that we can use to fill that seed array "s[]"?

They are suggesting to use the splitmix64 algorithm to do that. As your google is broken I will find it for you.... ah yes, here https://github.com/svaarala/duktape/blo ... litmix64.c

Code: Select all

uint64_t x; /* The state can be seeded with any value. */

uint64_t next() {
	uint64_t z = (x += UINT64_C(0x9E3779B97F4A7C15));
	z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
	z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
	return z ^ (z >> 31);
}
See how that goes?
1) Set the "x" value of splitmix64 to whatever 64 bit seed value you have.
2) Call the next() function of splitmix64 to get the next random output in it's sequece.
3) Use that output to fill in an element of the xoroshiro1024plusplus state array s[].
4) Repeat 2) and 3) until the s[] array is full.

Now you have seeded xoroshiro1024plusplus and can use it's next function for your proper PRNG sequence.

This way we get a nice "high entropy" jumble of bits to seed xoroshiro1024plusplus which are for sure never all zero.
Memory in C++ is a leaky abstraction .

User avatar
jahboater
Posts: 6299
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 12:45 pm

Heater wrote:
Sun Oct 04, 2020 12:38 pm
This way we get a nice "high entropy" jumble of bits to seed xoroshiro1024plusplus which are for sure never all zero.
That is 64-bits of entropy ?
What about:-

Code: Select all

  ssize_t nread = 0;
  const int fd = open( "/dev/urandom", O_RDONLY );
  if( fd >= 0 )
  {
    nread = read( fd, s, 16 * 8 );
    close(fd);
  }
  if( nread == 16 * 8 )
    OK ...
/dev/urandom should always have 128 bytes of entropy even straight after boot.
Pi4 8GB running PIOS64 Lite

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 1:20 pm

jahboater wrote:
Sun Oct 04, 2020 12:45 pm
That is 64-bits of entropy ?
Yes.

Where my "entropy" there means: "lack of order or predictability" as per the dictionary definition. By which I mean that :

0000000000000000000000000000000000000000000000000000000000000000
or
1111111111111111111111111111111111111111111111111111111111111111
or
1111111111111111111111111111111100000000000000000000000000000000
or
0000000000000000000000000000000011111111111111111111111111111111

and the like would be considered "low" entropy. But something that looks like:

1111011101110101000100111001000100101110010111100100100111001010

Would be considered "high" entropy.

The important point being we end up with sixteen 64 bit integers that all look like like that nice random looking high entropy thing, and all different, rather than a bunch of boring looking low entropy looking things.

There is a measure of entropy that depends on the length of a string required to describe the thing. For example the all zeros case could be "64*0", the half ones and half zeros case above could be described as "32*1, 32*0" and so on.

The high entropy example requires a longer string to describe it. Like: "4*1, 0, 3*1, 0,3*1, 0..." etc. Clearly this is a longer description and hence a higher entropy.

All of which is related to how well one can compress the data. If you gzip a file full of ones it will compress a lot more than a file full of high entropy numbers.

This is perhaps a slightly different notion of entropy that typically used when referring the number of bits one has available from a real random noise source.

What about:-

Code: Select all

  ssize_t nread = 0;
  const int fd = open( "/dev/urandom", O_RDONLY );
  if( fd >= 0 )
  {
    nread = read( fd, s, 16 * 8 );
    close(fd);
  }
  if( nread == 16 * 8 )
    OK ...
/dev/urandom should always have 128 bytes of entropy even straight after boot.

Looks OK to me.
Last edited by Heater on Sun Oct 04, 2020 1:46 pm, edited 1 time in total.
Memory in C++ is a leaky abstraction .

dsyleixa123
Posts: 1028
Joined: Mon Jun 11, 2018 11:22 am

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 1:44 pm

great, now I understand it much better! thanks guys!

Heater
Posts: 16854
Joined: Tue Jul 17, 2012 3:02 pm

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 1:47 pm

Excellent. Easy peasy eh.
Memory in C++ is a leaky abstraction .

User avatar
jahboater
Posts: 6299
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 4:13 pm

Heater wrote:
Sun Oct 04, 2020 1:47 pm
Easy peasy eh.
If only ....
:)
Pi4 8GB running PIOS64 Lite

ejolson
Posts: 6028
Joined: Tue Mar 18, 2014 11:47 am

Re: Random Number Generator Using Build In Hardware

Sun Oct 04, 2020 4:14 pm

Heater wrote:
Sun Oct 04, 2020 12:38 pm
Putting a random delay between those calls to millis is also not a good idea. It will take extra time for no useful purpose. Why not just use the random numbers you are going to use for the delay to make the seed values directly?
For the delays one could measure the time taken to type each character of the pangram "the quick dog jumps over the lazy brown fox." This would have the useful purpose of teaching some typing.

Alternatively, one could use the first call to millis() to select between different lines in the Communist Manifesto and again measure the time between typing each character. For better randomness, be sure to use the original German and not a translated version.

Speaking of pseudo-random numbers, there is also the 32-bit generator used in the original editions of Unix

viewtopic.php?p=1713336#p1713336

which I've been trying to port so it runs on the version of Pascal included with the Waterloo language package for the 8-bit Motorola 6809. It seems Fido's birthday is coming up soon, the ban on computing is about to end and as a present I want to make an authentic version of Hunt the Wumpus for the SuperPET.

Fortunately VICE is running well on the Pi and accessible through remote desktop using the latest version of xorgxrdp. Development is underway but the only available integer data type is signed 16-bit with add, subtract, multiply, divide and modulo operations. It is, of course, important (aside from any psychokinetic influences) to get exact reproducibility of the desired pseudo-random sequence.

Any help to use 2's compliment 16-bit signed arithmetic to exactly implement the random number generator

Code: Select all

static	long	randx = 1;

srand(x)
unsigned x;
{
	randx = x;
}

rand()
{
	return(((randx = randx*1103515245 + 12345)>>16) & 077777);
}
where unsigned is a 16-bit data type and long is a signed 32-bit integer would be very much appreciated.

Return to “C/C++”