Jump to content
43oh

Generating random numbers.


Recommended Posts

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

Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

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

Link to post
Share on other sites

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

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...