Searching \ for '[SX] How to generate random ' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/ubicom/lib/math/index.htm?key=random
Search entire site for: 'How to generate random'.

No exact or substring matches. trying for part
PICList Thread
'[SX] How to generate random #'s'
2005\10\02@204547 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Hi everyone, I'm trying to figure out how to generate a table of random numbers with an SX.

Is it possible to simply read or copy the random values that the data registers initialize to?

Using the analog comparator on 2 RC circuits of different frequencies would probably generate effectively random bits, but there must be a better way.

Any ideas?

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\02@214450 by Peter Van der Zeen/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Peter Van der Zee wrote:

Hi Electro;
Although I'm not going to give a good mathematical answer to your question, your idea of using the apparently random state of registers on power up is NOT a good idea. While those values may appear to be rendom, they generally, or at least often, are the same every time you boot. So not good randomness there.

I have not really studied this subject; there may be some good pointers in the SXList site.

If I needed random numbers, and it depends on the number of bits required, I would probably make a Pseudo Random Sequence Generator by simulating a sufficiently long shift resgister in software, and provide the appropriate feedback taps for the maximal configuration. This is pretty easy really. Google can probably get you there, and then you will need to do some experimentation.

Once I had that generator running, I would simply pick off a number when I needed one.

While this process may not be mathematically pure, or correct, I believe it will at least get you close.

Cheers,
Peter (pjv)
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90115
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\03@002525 by PJMontyn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, PJMonty wrote:

Electro,
Here's the routine I got from the SxList:

;---------------------------------------------
; 7/02/04 PJM - This function uses a linear congruential
; sequence generator as a pseudo-random number generator.
RandomNumGen
;Rnew = Rold * 221 + 53
;221 = 256 - 32 - 4 + 1
;256 can be eliminated
;so we need to calculate Rnew = Rold * (1 - 32 - 4) + 53 using
;truncating arithmetic
;or Rnew = Rold * (-32 - 3) + 53
               clrb                C
               rl                        RandomNum                                        ;RandomNum' = 2*RandomNum and carry contains the original MSb
               mov                        W, <>RandomNum      
               and                        W, #$E0                                                ;W = 16*RandomNum'=32*RandomNum
               rr                        RandomNum                                        ;restore RandomNum
               add                        W, RandomNum              
               add                        W, RandomNum
               add                        RandomNum, W                        ;RandomNum'' = 32*RandomNum + 3*RandomNum
               not                        RandomNum                                        ;RandomNum''' = 255-RandomNum'' = -1 - RandomNum''
               mov                        W, #54
               add                        RandomNum, W                        ;RandomNum''''=-1 - 32*RandomNum - 3*RandomNum + 54

"RandomNum" is a variable (in this case it's a global variable) that holds the value of most recent random number generated between interations.  Every time you call this routine, you'll update the value of "RandomNum".  If you want to find the original, search for "congruential" on the SxList site.  Make sure you go to the page with the SX code and not the PIC code.

[list]Thanks,
PeterM[/list]
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90121
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\03@073754 by Jon Williamsn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Jon Williams wrote:

The RANDOM function is SX/B is very simple, yet has good distribution only repeats after 256 iterations.

; seed = seed * 5 + 123
Random
 MOV W, seed
 ADD seed, W                
 ADD seed, W                  
 ADD seed, W                
 ADD seed, W                  
 ADD seed, #123
 RETP
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90144
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\03@074021 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Thanks PJ, I certainly wouldn't have thought to search for "congruential"
I still need a way to generate a random seed though, or add some non-volatile memory to hold the current value of RandomNum when the device is shut off.

When I get home I am going to try using the analog comparator with no input on either pin.

At worst case I can have a ADC digitize accross a range that exceeds its sensitivity and read the least significant bit.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90145
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\03@093143 by Coriolisn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Coriolis wrote:

The easiest way to generate a seed is by an external random input such as a button press. Automated means for generating the seed exist but are much more involved, and typically involve sampling a noise source such as a zerner diode, look here for a brief explanation.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90156
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\03@094512 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Thanks Paul, I'm sure the button press with the Psuedo-random software generator is the most sensible thing to do.

The problem with stuff like the zener noise is that the noise has to fluctuate randomly above and below the TTL or CMOS voltage threshold.

If I can come up with an elegant hardware solution I'll report back
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90159
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\04@171142 by jgjonolan/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, jgjonola wrote:

Please let me know if you find a solution for true random numbers, as i have an application that could be using in a gaming device, but the specs require true RN.  When programming in C on a pc, i have used the "mersenne twister" (google it) method, and it passed all of the tests, but i cant replicate that on the SX.

Thanks!
John Gjonola
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90379
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\04@174920 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

I began an experiment last night that seems to have promise.

I hooked up the 4MHz oscillator that came with the SX Tech KIT to 2 of the pins on an SX18, with one pin configured to input and the other to output. The SX is clocked at just above or below 4MHz Using one of the IRC_CAL directives. (I'm not sure which will work better yet.

The output pin kicks the clock to start it running and the input pin polls the signal.

From the SX temporal reference frame the resonator output will appear as a slowly varying sine wave with a frequency equal to the difference between the actual clock speed and 4.00MHz.

I believe that if you were to poll the input pin at full speed you would see a bunch of 1's in a row followed by a few random bits, then a bunch of zeros in a row followed by a few random bits and back to 1's again.

The period of randomness occurs when the oscillator voltage is very close to the CMOS threshold (2.5V), making it difficult for the SX to decide if the value is high or low.

I plan to set a voltage transition interrupt on the pin to mark the point at which the oscillator output crosses the CMOS threshold, and then poll at a frequency that enables me to repeatedly sample the oscillator output when it is very close to the 2.5V portion of its phase.

This will be done in software by a calibration routine that "wastes" progressively more time between polling events until a predefined criteria for randomness is met. Since in the long term the polling rate would surely walk off the transition into the high or low regions, I plan to generate the random numbers one byte at a time with each determination initiated by a transition interrupt.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90391
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\05@064137 by Coriolisn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Coriolis wrote:

[Quoting: "jgjonola"]
Please let me know if you find a solution for true random numbers, as i have an application that could be using in a gaming device, but the specs require true RN. When programming in C on a pc, i have used the "mersenne twister" (google it) method, and it passed all of the tests, but i cant replicate that on the SX.

Thanks!
John Gjonola

Sorry to tell you but mersenne twister is a psuedo-random number generator. Granted it repeats only after 2[sup]19937[/sup] ? 1 iterations, but it still isn't a random generator. The only way to generate truely random numbers is by sampling a physical process which is inherently random, such as nuclear decay, or my favorite, a> (though it is technically used as a random number seed generator since there is a strong correlation of numbers between two closely sampled images, though a hash can take care of that (and also add a level of complexity and computational time)). If you are interested in other psuedo random number generators look here. If you want strong random numbers, it is both memory and computationaly intensive, and you will likely need a dedicated SX as a co-processor generating them.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90443
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\05@081215 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

This is way off topic, but since you mentioned cryptography and random numbers I think it might be possible to build an unbreakable encryption sheme based on Bell's theorum.

The short story is that if you generate two simultaneous photons from the same source they will have the same phase at a later time regardless of the paths traveled.  The phase of a photon can be rendered completely random by repeated scattering events.

Here's the encryption scheme:

A central source generates two phase-entangled photons and sends one to each of our computers down their respective optical cables. The phase of the photons are randomized by scattering in the cable, but are measured to be the same by ploarizers at the point where the photons reach our computers.

We use the phase of the photon as an encryption key.

This effectively creates 2 identical private keys for RSA encryption without the need of a public key, and nothing, not even the source that generated the photons, could predict what the phase would be at the time of measurement.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90464
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\05@095608 by Tracy Allenn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Tracy Allen wrote:

Another way to captialize on ground bounce noise in the threshold,  is an RCTIME command, where the capacitor and resistor are purposely poor, and the wiring to the ground connection loops around to form a good (bad!) ground loop, calculated to pick up a lot of AC,  processor emi, and the local radio station.    For a long RCTIME that approaches the threshold asymtotically, the slop in the least significant bits can be a random seed.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90488
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\05@111709 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Hi Tracy,
That sounds like it might be a better idea then what I am doing since it uses only one pin and doesn't require elaborate timing.

The unfortunate thing about both methods is that you have to waste a a lot of time waiting for the signal level to be near the logical voltage threshold and then only get a few random bits.

What if we set an input pin for CMOS level and then piped in Vdd/2 from a voltage divider?
I suppose it would be a little over or under Vdd/2 because of the resistor tolerances, but it may be close enough.
Maybe adding a bit of ringing from something like a ground bounce or coupling unlike inductors in series would mess the signal up enough that we could read random bits at will.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90504
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\05@120625 by Tracy Allenn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Tracy Allen wrote:

Yes, it should be possible to bias the input near the threshold.   Remember, there is a sharp peak in current consumption by the SX chip when the pin passes within 0.1 volt of the threshold, so it would be a good idea to have means to force it to an output when it is not being used for noise.  Hi resistances.  The nice thing about the capacitor is that is assure that the system passes through the threshold.  Maybe a combination scheme where the capacitor is connected to a V divider that puts the initial voltage near the treshold to save time.

Another idea would be to hook up the comarator with direct positive feedback from the output to (+), and with (-) at V//2.   The propagation delay in the comparator makes it oscillate with lots of jitter at around 40 mhz.  In any case, sampling the comparator output would be a pretty fair crap shoot.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90513
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\05@130054 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

I tried sampling the comparator by itself with nothing attached to the pins but it just chose one pin as the high one and stuck with it.

I will try feeding in the input from a voltage divider when I get home.

As far as pushing the pin off the threshold when it's not in use for noise detection,  a transistor could control the voltage into the noisemaker so it can be turned off when not in use.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90523
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\05@234248 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

I got home after midnight tonight, but had to try it.

With the comparator hooked up to nothing it actually generates pretty random bits, but it seems to favor 0 a little over 1.

I have the port A registers attached to 4 diodes and am sampling the comparator output with the following code:

GetNumber sb rb.0
jmp BlueGreen
jmp RedYellow
BlueGreen
sb rb.0
jmp Blue
jmp Green
RedYellow
sb rb.0
jmp Red
jmp Yellow
Blue
mov LIT, #%0001
ret
Red
mov LIT, #%0010
ret
Green
mov LIT, #%0100
ret
Yellow
mov LIT, #%1000
ret
After returning it moves the value in LIT to ra and lights whatever diode corresponds to the bit that is set.

The favoritism of zero over one manifests itself as the blue diode being lit a little bit more than the others.

Grounding one of the two comparator inputs will consistently cause it to choose the other input as high.

Now here's the odd part:

When I momentarily tap Vdd to pin rb.2 (through a resistor) it correctly lights the yellow diode, but then it stays lit for about 24 seconds before returning to semi-randomness. This is at 4Mhz with the SX key detached.
It takes longer for slower clock speeds out to around 38 seconds at 32kHz.

If I tap rb.2 with Vdd/2, it stays lit for about half the time before rb.0 starts flickering again.

Just in case you thought this couldn't get any more bizzarre, when I tap rb.3 with Vdd/2 the comparator output goes to zero and stays there indefinitely.

I have attached the rest of the code.
The circuit is nothing more than an SX18AC/75 powered by an L7805 and Guenther's recommended capacitance circuit (without the back EMF protection diodes). There is also a programming header and 4 diodes hooked up to the ra register. with a 570 ohm resistor on the ground side to keep them from frying. The voltage divider is constructed from 2 4.7k resistors, and there is a 10k resistor plugged into Vdd.

-Electronegativity signing off.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90565
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\06@063911 by Coriolisn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Coriolis wrote:

[Quoting: "Electronegativity"]
 
With the comparator hooked up to nothing it actually generates pretty random bits, but it seems to favor 0 a little over 1.


Sounds like a job for a binomial stochastic transform. If you can plot the density function of the numbers produced to see what the probability of p and q are (the likelyhood of it being a 0 or 1 respectively), you can do several things to get it to be p=q=0.5, the easiest is the rejection method. Say p=0.75 and q=0.25, if you throw away every 3rd 0 you obtain (don't use and get a new value instead), p and q will become 0.5.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90587
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\06@081051 by Tracy Allenn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Tracy Allen wrote:

I'm not clear to me on how the pins were connected for the test you described.  Was at least one of the inputs floating, not connected to anything?  
The comparator can be made into a high frequency oscillator, by connecting a wire from RB0 to RB1, and by connecting RB2 to a medium voltage.  That could be ~Vdd/2 from a potentiometer between Vdd and Vss.  The oscillator frequency is determined by the comparator propagation delay. It has lots of jitter and probably 1/f noise and temperature dependence too.    It would be like having a madman sitting there pushing a button. The program could sample that occasionally to get  random bits or to spice up a psuedo random sequence.  
I looked back at some old notes, reported in this thread,
http://forums.parallax.com/forums/default.aspx?f=7&m=58271
and the period of the oscillation was around 40 nanoseconds on a 5 volt supply, an oscillation frequency of around 25 megahertz.  The period is determined by the two propagation delays, how long it stays in the "1" state vs how long in the "0" state.  
-- The ratio of high to low  is affected by the threshold applied to RB2 and becomes highly asymmetric if the potentiometer takes the rb2 threshold near Vss or Vdd. One way to equalize 1s and 0s would be to feed the oscillator output into the RTCC and sample the /2 bit of that.   Or there may be some way to equalize in software like Paul suggested.

-- A program sampling at anywhere near the 40nS period could run into serious aliasing problems.  But for occasional samples (at  more than 10 microsecond apart, say), the statistical correlation should drop practically to zero.

Another way to use the comparator, instead of a direct wire connection from rb0 to rb1, would be a high value resistor (~1M) there.   That more gentle negative feedback should bias the comparator as an op amp without the oscillation.  Couple in an intrinsic or external noise source, and it would be amplified up  to digital level for sampling.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90596
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\06@091836 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Hi, first a correction: rb.3 should be rb.1 in the post above.

Paul, that is a reasonable suggestion but it would make it difficult to port the code from one chip to the next since I suspect that each chip would require a slightly different transform, and even within a given chip the local temperature and electromagnetic environments could affect the weighting towards 1 or 0.

Tracy, there is nothing connected to any of the comparator pins. rb.1 and rb.2 are initialized as inputs and set low.
rb.0 is initialized as an output, set low, and is configured to output the comparator value.

I will try your oscillating comparator trick when I get home.

Does anyone have a clue why the strange behavior I described at the end of my last post occurs?

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90603
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\07@101530 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Report from last night:

Tracy's "Madman circuit proved more random than leaving the comparator pins open, and seened to improve a bit further by using a lower voltage on pin rb.2, but showed a slight favoritism of 1 over zero.

This could probably be cleaned up nicely by Paul's binomial stochastic transform.

Whenever the device resets it could run a calibration cycle where it samples a large number of bits and determines the proper number of high or low results to throw away.

If using up 3 valuable pins on port B is a problem then the madman circuit can be connected or disconnected by a transistor that is actuated from one of the port A pins.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90726
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\07@143452 by Tracy Allenn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Tracy Allen wrote:

The symmetry wrt 0 and 1 could be improved by connecting rb.0 comparator output to the rtcc input configured as a counter (no prescaler), and sample the /2 bit of rtcc.   The divide by two (~12 megahertz) would render it symmetric (equal probability of 0 and 1 without math tricks).  Assuming you don't need rtcc for something else!!

Sampling it rapidly would lead to aliasing with the processor clock.   The longer the program waits between sample bits, the less the correlation between them.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90765
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\07@192558 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

I ordered a few inductors, and when they come I am going to add an asymmetric coupled LC circuit in the hopes that the added complexity will lead to true chaos.

Even as it is it's good enough to generate a sufficently random seed values.

One could easily imagine a Psuedo-random generator that used 3 or 4 variables that were all randomly seeded.

Good enough for game purposes, but maybe not aerospace engineering  :smilewinkgrin:

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90789
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\09@185253 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Ok, I know I'm beating this topic to death, but I have one more thing to say.

There is a simple way to avoid having to normalize the data for a process that favors either zero or one.

You collect 2 bits in a row.
If they are both positive or both negative you throw them out and collect 2 more bits.
If the first one is negative and the second positive then the result is zero.
If the first one is positive and the second one is negative then the result is one.
I think this will yield random bits even for a semi random system that is more likely on the average to be high or low.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m90934
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\10@125843 by Coriolisn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Coriolis wrote:

Ive heard of that algorithm, I think random.org talks about it.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m91055
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\10@135506 by Electronegativityn/a
flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Hi Paul.

Yes, that is true, and I realized it when I came up with the algorithm, but I think it works if you poll at a rate which is slower than and asynchronous to the rate at which the bits are generated.
Tracey estimated that the comparator would oscillate at ~40 Mhz. I am clocking the SX at 32kHz and not even sampling every cycle.

The logic is as follows:

Imagine a source that generates more zeros than ones.
This could happen if the crossover point of a sinusoidal voltage is lower than the logical transition voltage.
In this case the sine wave would spend most of its time below the transition threshold and only occasionally peep above into the region corresponding to a one.

If you sample a source like the one above at a rate which is slower than and asynchronous to the period of the sine wave you would get a lot of zeros and a smattering of randomly distributed ones.

Applying the "two-bit" algorithm to a case in which nine times as many zeros as ones are randomly generated would have the following effect.

81% of the time you would get two zeros in a row and discard the result.
1% of the time you woulg get 2 ones in a row and discard the result.
9% of the time you would get first a zero then a one  (90% X 10%)  = 0.
9% of the time you would get first a one then a zero (10% X 90%)   = 1.

I have it up and running on a breadboard at home and at least to my eye it appears perfectly random.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m91067
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\10@160451 by PJMontyn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, PJMonty wrote:

Electronegativity,
You mention that you have it wired up and running, and it appears random to your eye.  Well, take your eye out of the loop and let a computer tell you if you have a random number generator or not.  On your testbed, add some RS-232 code (and whatever level shifting hardware you need) and have your testbed send the values from the random number generator to your PC.  On the PC side, write a small program to receive the numbers from the SX and create a histogram from them.  
The program is pretty much dead simple - for each number that comes in, use it as an index into an array of 32 bit integers which you increment.  Run the program for some finite length of time.  For example, try running the program for a minute, ten minutes, and an hour.  Better yet, use the data rate to calculate a number of samples that takes close to those times to collect, but which is evenly divisible by 256.

At the end of each run, have the program write out a simple comma or tab delimited text file of the final value of each entry in the array.  You can now open that text file into Excel and do things like graph it, or calculate the min, max, and mean values, etc, etc.  Since you have collected a number of samples evenly divisible by 256, you should have (theoretically) the same value in each of your PC's array entries.  If not, you can see which way you are weighted.

[list]Thanks,
PeterM[/list]
PS - I did something similar to this to verify that the pseudo-random number generators on SxList.com provided an even distribution.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=90112#m91084
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\10@162215 by James Newtonn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, James Newton wrote:

You know, the pseudo-random routines on sxlist ( at http://www.sxlist.com/techref/ubicom/lib/math/rand_sx.htm ) really do work pretty darn well and don't require any io pins...

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91096
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\10@205216 by Tracy Allenn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Tracy Allen wrote:

Electro,
I was recalling fuzzily.  The comparator with direct feedback oscillates with a period of about 40 microseconds, a frequency of around 25 megahertz.  What do you plan to do with the inductors to make it more chaotic?  
Donald Knuth's volume, "Seminumerical Algorithms", goes into great depth on different random number generators and statistical tests that they should pass.   The  introductory lesson that sticks in my mind is, paraphrased, " never choose a random number algorithm randomly".

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91146
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\11@081534 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

PJ, I intend to use this to create a table of random color sequences for a tiny hand-held Simon Sez game.
As long as the user cannot perceive any divergence from randomness it is good enough.

James, even if the Pseudo-random code is perfect I still need to generate a random seed or the colors will always be the same. I know I could have the user press a button and read a seed value of the RTCC, but I want them to be able to start a new game by pressing only a single reset button instead of having to reset and then press a second button.

Tracey, my plan was to randomly choose a random number generator.  :tongue:
I figured that by slapping enough mismatched inductors and capacitors together I could build something completely chaotic.

I don't think I will need to do that though, since your oscillating capacitor idea can be implemented here in a very simple way.

I can bridge rb.0 and rb.1 with the emitter and collector of a transistor. The base is hooked up to ra.3.
Rb.0 and rb.1 are also hooked up to blue diodes, while rb.2 is attached to a red diode. When ra.3 turns on the transistor it sets up the oscillation of the comparator.

The "marginal voltage" on rb.2 is supplied by the fact that there is a higher forward voltage drop accross the blue diodes than the red. Once the table of random numbers is generated, I drop the voltage on ra.3 which disconnects the bridge. In this way I can use your method without using up any pins at all since ra.3 is subsequently set as an input and used to read a button.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91189
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\11@122933 by James Newtonn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, James Newton wrote:

I may be wrong on this, but I'm pretty darn sure you can just read the RTCC after you get started up. The startup time is NEVER going to be exactly the same after each startup. Minor changes in supply voltage will guarrenty that the chip will have a different value in RTCC every time.

Try it?

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91243
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\11@124601 by PJMontyn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, PJMonty wrote:

Electronegativity,
I figured that since you wanted a true random number generator, you needed to be sure it was truly random.  Since you're just making a game, any old psuedo-random number generator will be more than adequate.  You're killing yourself with all this extra effort.

James' suggestion will work fine, but can be made even better.  Run a pseudo-random number generator in the RTCC.  Assuming a reasonably fast interrupt rate, you will be generating hundreds or thousands of random numbers per second.  Whenever the user presses a button to start a new game, there's basically no way they will ever be accurate enough to predictably get the same random seed each time.  I used this exact technique on a project and it works great.

[list]Thanks,
PeterM[/list]
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91247
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\11@133832 by Tracy Allenn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Tracy Allen wrote:

I agree with others that a psuedo random generator seeded from RTCC would be adequate for your game application.

I was pursuing the comparator idea as a curiousity.   Pursuing it a little further, I don't understand the need for the transistor. I was trying to minimize the amount of external parts required down to basically two resistors.  Why not just turn the comparator on and off via the comparator mode bits, CMP_B?  You could even use it then for other i/o.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91255
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\11@144658 by Electronegativityn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Electronegativity wrote:

Hi Tracy, I have each of the port b registers hooked up to a diode, and each of the port a registers hooked up to a switch.

There are 2 blue diodes on top, 2 red on the right, 2 green on the bottom, and 2 yellow on the left.
I plan to have the game produce little light shows to celebrate the players progress every time the length of the memorized sequence increases by 4 colors.

Since it is an SX20 there are no more I/O pins available.
If I bridge the rb.0 and rb.1 pins then I will not be able to light the 2 blue diodes independently, so the transistor is there to let me open or close the bridge.

PJ, I like the idea of running the Psuedo-random generator in the RTCC.
If I understand you correctly the output of the generator is input into the RTCC and incremented some number of times before being pulled out to use as a new seed. This seems like it would take care of the problem that a psuedo-random generator always produces the same series of numbers from a given initial value.

James, I always assumed that the RTCC starts at zero and increments along with the program counter.

Is this wrong?

If I ran the following program:

RESET Start
Start nop
nop
nop
Wouldn't the RTCC hold a value of 3?

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91267
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

2005\10\11@162231 by PJMontyn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, PJMonty wrote:

Electronegativity,
Actually, what I do is run the random number generator in the RTCC interrupt itself.  I store the result (an 8 bit number) in a variable.  Since the RTCC is running at a (presumably) fast rate, the pseudo-random sequence is being run through hundreds or thousands of times a second.  Given that there is no way to predict when the human will push the button to start the game, and also given that no human is going to be accurate enough to push the button the exact same time after starting the device, there is essentially zero chance for the same random number to appear.

Furthermore, your code could test the condition of the button at start up and not accept the button as being pushed until it has at least seen it not pushed once in the primary loop.  This would prevent the user from holding the new game button down at startup.  
In addition, the whole time your game is being played, the RTCC is still running and the pseudo-random number generator is still cranking out new values on each interrupt.  Because of this, even if the user were able to somehow get the unit to give the same game each time they started it up, they would never be able to get the same second game unless they took the exact same amount of time to play the game and hit the "New Game" button.

Basically, imagine a roulette wheel behind a curtain.  It's turning at high speed and someone is watching it.  Periodically, someone in front of the curtain asks what number the wheel is at, and the person in back notes the number and passes it to the front.  This is what is happening by simpy having the pseudo random number generator running on each interrupt all the time.  Periodically, the user will push the "New Game" button and the foreground code will look at the variable with the current random number, but there is zero correlation between the current state of the generator and the time the user pushes the button, hence the human variable guarantees a truly random number generator since they themselves are the true random element.

[list]Thanks,
PeterM[/list]
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=90112#m91283
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)

More... (looser matching)
- Last day of these posts
- In 2005 , 2006 only
- Today
- New search...