| | 246 | == Random Number Generation with LFSR == |
| | 247 | {{{#!go |
| | 248 | \\ Jacob Morin |
| | 249 | |
| | 250 | // Call with either 1 or 3 command arguments: |
| | 251 | // First command argument: amount of loops |
| | 252 | // Second command argument: start |
| | 253 | // Third command argument: end |
| | 254 | package mainimport ( |
| | 255 | "fmt" |
| | 256 | "os" |
| | 257 | "strconv" |
| | 258 | )func lfsr3(sequence chan uint8, start int, end int, repititions int) { |
| | 259 | for i := 0; i <= repititions; i++ { |
| | 260 | //starting value |
| | 261 | var seed uint16 = uint16(i) |
| | 262 | //keeps track of the value at any given time |
| | 263 | var lfsr uint16 = seed //number of times it takes for the lfsr to revert to its start state |
| | 264 | var period int = 0 |
| | 265 | //last bit of lfsr |
| | 266 | var value uint16 = 0 |
| | 267 | //number of 0's in lfsr |
| | 268 | var numZeros int = 0 |
| | 269 | //number of 1's in lfsr |
| | 270 | var numOnes int = 0 stop := false |
| | 271 | //until LFSR goes back to its start state (stop will become true) |
| | 272 | for stop != true { |
| | 273 | //increments count |
| | 274 | period++ //shifts |
| | 275 | lfsr ^= lfsr >> 7 //7 right |
| | 276 | lfsr ^= lfsr << 9 //9 left |
| | 277 | lfsr ^= lfsr >> 13 //13 right if (period > start) && (period < end) { |
| | 278 | //adds last bit to sequence |
| | 279 | value = lfsr & 1 |
| | 280 | if value == 0 { |
| | 281 | numZeros++ |
| | 282 | sequence <- 0 |
| | 283 | } |
| | 284 | if value == 1 { |
| | 285 | numOnes++ |
| | 286 | sequence <- 1 |
| | 287 | } |
| | 288 | } //if LFSR reaches startState, make stop true to exit the loop |
| | 289 | if lfsr == seed { |
| | 290 | stop = true |
| | 291 | } |
| | 292 | } }}func main() { //makes channel for the random number |
| | 293 | sequence := make(chan uint8) //command line |
| | 294 | args := os.Args |
| | 295 | len := len(args) //first argument in command line determines how many times to loop |
| | 296 | if len > 1 { |
| | 297 | repititions, err := strconv.Atoi(args[1]) |
| | 298 | if err != nil { |
| | 299 | fmt.Println("error") |
| | 300 | } |
| | 301 | // assumed values |
| | 302 | start := 6000 |
| | 303 | end := 7000 // second argument in command line overrides start |
| | 304 | // third argument in command line overrides end |
| | 305 | if len > 3 { |
| | 306 | y, err := strconv.Atoi(args[2]) |
| | 307 | if err != nil { |
| | 308 | fmt.Println("error") |
| | 309 | } |
| | 310 | start = y z, err := strconv.Atoi(args[3]) |
| | 311 | if err != nil { |
| | 312 | fmt.Println("error") |
| | 313 | } |
| | 314 | end = z |
| | 315 | } |
| | 316 | //calls function |
| | 317 | go lfsr3(sequence, start, end, repititions) printSequence := "" |
| | 318 | //Call lfsr3 'repitions' number of times |
| | 319 | for i := 1; i <= repititions; i++ { |
| | 320 | //number of values per sequences |
| | 321 | for j := 0; j < 999; j++ { |
| | 322 | //receives and prints value |
| | 323 | value := <-sequence |
| | 324 | printSequence += strconv.Itoa(int(value)) |
| | 325 | } fmt.Println("At", i, "got random number:", printSequence, "\n") |
| | 326 | printSequence = "" |
| | 327 | } |
| | 328 | }} |
| | 329 | }}} |
| | 330 | |