Random Generator

This is a discussion on Random Generator within the C Programming forums, part of the General Programming Boards category; Hi all, i have a problem which i was hoping someone could help me with. I am attempting a task ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Random Generator

    Hi all, i have a problem which i was hoping someone could help me with. I am attempting a task that creates a set of 3 random numbers which are stored in three separate characters. These numbers are only between 0 and 3 so will only ever use a max of 2 bits. Given this my problem, i need to display each of these numbers on a set of 8 LEDS simultaneously using a global interrupt every 250ms. Each random number is generated on 2 LEDS with a space between each, which the space LED is not active. Eg. 11011011 where the 11's are where i display each random number. My interrupt works fine, the only problem is i have no idea how to display 3 different values on specific bits of the Port they are on at the same time. Any suggestions/help would be appreciated? I'm using an ATMEGA8 device, and my knowledge of C is relatively basic but i do have have an understanding of pretty much everything else im doing at the moment.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know the bitwise operators? If not, you should learn them.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Yes i do, but still unsure how to go about it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you've got three steps to do:
    1. Build a number that has your first random number in bits 6 and 7 and the rest zero.
    2. Now change bits 3 and 4 to be your second number.
    3. Now change bits 0 and 1 to be your third number.

    The first one is pretty easy; the second one might take a bit more thinking, so look at your truth tables. The third is the same as the second, more or less, so that won't take long either.

    Then put the three together in order and you're done.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    Thanks for the help, i managed to do it in a similar way but i was a little unsure how to manipulate singular bits to a value, but i just set or unset bits based on what the random value was.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random generator 0 1
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 06-30-2009, 08:44 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM
  4. looking for a random # generator
    By w/ possion? in forum C Programming
    Replies: 4
    Last Post: 02-28-2003, 06:58 AM
  5. Random Name Generator
    By jdinger in forum C++ Programming
    Replies: 6
    Last Post: 04-11-2002, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21