Separating a PORT into in/out and getting random nums?
Why are you not using the rand() function from the avr standard libraries?

Library documentation, stdlib.h


  void function() { PORTD = (uint8_t)rand(); }


In the second part of your question, just split them up.
If you can use pins 2, 3, and 4 of PORTA, do:


  PORTA = randonNumber &amp; ( (1 &lt;&lt; 1) | (1 &lt;&lt; 2) | (1 &lt;&lt; 3) );


Basically I would suggest for you to study the different ways in which you can map source bits to destination bits in C through the use of bitwise operators.