EngIP 31 Posted May 2, 2011 Share Posted May 2, 2011 Would someone be kind enough to post some code for generating a random (or pseudo random) number. I can do this in C but only using the time header to seed, and I'm guessing the MSP needs a different method. Just barebones code on generating a number between 0 and x would be fantastic, and undoubtedly save me some searching. Many thanks Guy Quote Link to post Share on other sites
NatureTM 100 Posted May 2, 2011 Share Posted May 2, 2011 I don't know a lot about it, but you'll need to start with some sources of "randomness," and the more the better. Ones that come into my head right away are using a timer, internal temperature sensor or some other adc input, and floating input pin states. You could xor those together and use that to seed your random C function. Quote Link to post Share on other sites
RobG 1,892 Posted May 2, 2011 Share Posted May 2, 2011 I would also use built-in temp sensor, but instead of floating pin, I would use RC filter. Quote Link to post Share on other sites
GeekDoc 226 Posted May 3, 2011 Share Posted May 3, 2011 I would also use built-in temp sensor... Not all 430s have the temp sensor. This is an interesting problem, with so few available seeds. Quote Link to post Share on other sites
RobG 1,892 Posted May 3, 2011 Share Posted May 3, 2011 So how about some wacky RC or even 555 generator? Pick components that have high temperature coefficient and this thing will be highly unpredictable. Another option is a noise generator. There used to be a chip sold by Radio Shack, can't remember the number, but you can also build one using a Zener diode. Quote Link to post Share on other sites
bluehash 1,581 Posted May 3, 2011 Share Posted May 3, 2011 ... or a floating ADC pin. Quote Link to post Share on other sites
RobG 1,892 Posted May 3, 2011 Share Posted May 3, 2011 ...but the problem with floating pin is that it may get stable, for example if you put your board in a metal box, it's unpredictability is unpredictable Quote Link to post Share on other sites
MochaSatin 3 Posted May 10, 2011 Share Posted May 10, 2011 There is a simple procedure for calculating random numbers using integers. Look into Linear Congruential Generator. Quote Link to post Share on other sites
jsolarski 94 Posted May 10, 2011 Share Posted May 10, 2011 TI has an application note on the same subject http://focus.ti.com/mcu/docs/litabsmultiplefilelist.tsp?sectionId=96&tabId=1502&literatureNumber=slaa338&docCategoryId=1&familyId=911 it may be able to give a good start, i know using 2 independent clock sources may not be viable but its worth a look bluehash 1 Quote Link to post Share on other sites
gatesphere 45 Posted May 10, 2011 Share Posted May 10, 2011 If you need just a bit, you could use the method I used in my white noise generator... you could actually just sample however many bits you need and combine them to create a number. Check it out: http://www.43oh.com/forum/viewtopic.php?f=10&t=37 Quote Link to post Share on other sites
MochaSatin 3 Posted May 10, 2011 Share Posted May 10, 2011 Here is a small code snippet for the linear congruential generator. Adjust for you needs: #include int main() { int M = 256; int a = 65; int c = 27; int X = 1; int i; for(i=0; i<256; i++) { X=(a * X + c) % M; printf("%d ", X); } return 0; } Output: 92 119 82 237 72 99 62 217 52 79 42 197 32 59 22 177 12 39 2 157 248 19 238 137 228 255 218 117 208 235 198 97 188 215 178 77 168 195 158 57 148 175 138 37 128 155 118 17 108 135 98 253 88 115 78 233 68 95 58 213 48 75 38 193 28 55 18 173 8 35 254 153 244 15 234 133 224 251 214 113 204 231 194 93 184 211 174 73 164 191 154 53 144 171 134 33 124 151 114 13 104 131 94 249 84 111 74 229 64 91 54 209 44 71 34 189 24 51 14 169 4 31 250 149 240 11 230 129 220 247 210 109 200 227 19 0 89 180 207 170 69 160 187 150 49 140 167 130 29 120 147 110 9 100 127 90 245 8 0 107 70 225 60 87 50 205 40 67 30 185 20 47 10 165 0 27 246 145 236 7 226 125 2 16 243 206 105 196 223 186 85 176 203 166 65 156 183 146 45 136 163 126 25 116 1 43 106 5 96 123 86 241 76 103 66 221 56 83 46 201 36 63 26 181 16 43 6 161 252 2 3 242 141 232 3 222 121 212 239 202 101 192 219 182 81 172 199 162 61 152 179 14 2 41 132 159 122 21 112 139 102 1 RobG, bluehash and jsolarski 3 Quote Link to post Share on other sites
bluehash 1,581 Posted May 10, 2011 Share Posted May 10, 2011 Thanks. Linear Congruential Generator. Quote Link to post Share on other sites
RobG 1,892 Posted May 10, 2011 Share Posted May 10, 2011 You will still need to provide some random input number, otherwise you will get same results each time. Quote Link to post Share on other sites
rockets4kids 204 Posted May 10, 2011 Share Posted May 10, 2011 Another option I have used in the past where speed and small code size trump all else is a Linear Feedback Shift Register. This is something I learned from Don Lancaster back in the Apple 2 days. His particular recipe can be found here: http://www.tinaja.com/glib/atg1.pdf on page 1.1 dangpzanco and bluehash 2 Quote Link to post Share on other sites
bluehash 1,581 Posted May 10, 2011 Share Posted May 10, 2011 Thanks and welcome to the forums. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.