Generating random number won't work in a function

Hello folks. I am attempting to generate random numbers. I have a randomSeed statement in Setup. In Loop, if I put the statement "newrnumber = random (255);" then print newrnumber, I truly get a random number. However, if I put the "newrnumber = random (255);" statement in a function, the random number always comes back zero. I don't know why. I've tried putting the ramdomSeed statement in the function, the random number still comes back zero. I've put the code here - keep in mind that a lot of the statements are never executed, they are just here from trying different things. Your knowledge is appreciated.

FYI, I am using a Nano Super-Mini from AliExpress. So far the hardware has worked just fine.


  //Pin assignments

  const int DATA0 = 3;
  const int DATA1 = 4;
  const int DATA2 = 5;
  const int DATA3 = 6;
  const int DATA4 = 7;
  const int DATA5 = 8;
  const int DATA6 = 9;
  const int DATA7 = 10;
  const int NOTG1 = A0;
  const int ADDRESS1A = A1;
  const int ADDRESS1B = A2;
  const int NOTG2 = A3;
  const int ADDRESS2A = A4;
  const int ADDRESS2B = 11;

  //Variables defined

  String(LOWSTRING)="LOW";
  String(HIGHSTRING)="HIGH";
  int newrnumber=0;
  int x = 0;
  int y = 0;
  int z=0;
  int bit0 = 0;
  int bit1 = 0;
  int bit2 = 0;
  int bit3 = 0;
  int bit4 = 0;
  int bit5 = 0;
  int bit6 = 0;
  int bit7 = 0;

  //Setup/////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  void setup() 
  {
 
  
  // initialize pins as output.
  pinMode(DATA0,OUTPUT);
  pinMode(DATA1,OUTPUT);
  pinMode(DATA2,OUTPUT);
  pinMode(DATA3,OUTPUT);
  pinMode(DATA4,OUTPUT);
  pinMode(DATA5,OUTPUT);
  pinMode(DATA6,OUTPUT);
  pinMode(DATA7,OUTPUT);
  pinMode(NOTG1,OUTPUT);
  pinMode(ADDRESS1A,OUTPUT);
  pinMode(ADDRESS1B,OUTPUT);
  pinMode(NOTG2,OUTPUT);
  pinMode(ADDRESS2A,OUTPUT);
  pinMode(ADDRESS2B,OUTPUT);

  //Set up LED array

  byte LED[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  
  
  //Turn off all LEDs

  void leds_off();
  
  //Generate a random seed
  randomSeed(analogRead(0));
  
   
  Serial.begin(9600);
   
  }
  //END SETUP ////////////////////////////////////////////////////////////////////////////////////////////////////

  //Main loop/////////////////////////////////////////////////////////////////////////////////////////////////////

  void loop()

  {
    int bit[] = {0,0,0,0,0,0,0,0};
    
    
    //Generate a random number
    
    //Call genrnumber function
    int genrnumber ();

    Serial.print ("newrnumber= ");
    Serial.print (newrnumber);
    loop:
    goto loop;
    genbitsofbyte (newrnumber); //Call function that generates bit0 through bit7 variable values (1 or 0)
                                     // to be used to turn LEDs on or off
    Serial.print (bit[0]);
    Serial.print (bit[1]);
    Serial.print (bit[2]);
    Serial.print (bit[3]);
    Serial.print (bit[4]);
    Serial.print (bit[5]);
    Serial.print (bit[6]);
    Serial.println (bit[7]);

    delay (500);
    
  }
  //End of main loop /////////////////////////////////////////////////////////////////////////////////////////////

  //Function to generate a random number (newrnumber)/////////////////////////////////////////////////////////////

  int genrnumber()
    {
    
     newrnumber = random (0-255);
     //return newrnumber;
     Serial.println (newrnumber);

    }

  //Function to generates bit0 through bit7 variable values (1 or 0) to be used to turn LEDs on or off////////////

  void genbitsofbyte(int newrnumber)
    {
      int bit[] = {0,0,0,0,0,0,0,0};

      bit[0] = bitRead (newrnumber,0);
      bit[1] = bitRead (newrnumber,1);
      bit[2] = bitRead (newrnumber,2);
      bit[3] = bitRead (newrnumber,3);
      bit[4] = bitRead (newrnumber,4);
      bit[5] = bitRead (newrnumber,5);
      bit[6] = bitRead (newrnumber,6);
      bit[7] = bitRead (newrnumber,7);
    Serial.print ("In bit function");
    Serial.print (bit[0]);
    Serial.print (bit[1]);
    Serial.print (bit[2]);
    Serial.print (bit[3]);
    Serial.print (bit[4]);
    Serial.print (bit[5]);
    Serial.print (bit[6]);
    Serial.println (bit[7]);}




  void leds_on ()
  {
  //Set data all high for LEDs on
  digitalWrite (DATA0, HIGH);
  digitalWrite (DATA1, HIGH);
  digitalWrite (DATA2, HIGH);
  digitalWrite (DATA3, HIGH);
  digitalWrite (DATA4, HIGH);
  digitalWrite (DATA5, HIGH);
  digitalWrite (DATA6, HIGH);
  digitalWrite (DATA7, HIGH);

  //Set up the '139 for the address you want (latches 1-6)
  //Byte 1
  digitalWrite (NOTG1, HIGH); //set 1G high
  digitalWrite (ADDRESS1A, LOW); //set 1A, 1B, 2A, 2B for the latches you want to clock
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH); //data bits are now clocked into the '273.
  
  //Byte 2
  digitalWrite (NOTG1, HIGH); //set 1G high
  digitalWrite (ADDRESS1A, HIGH); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  //Byte 3
  digitalWrite (NOTG1, HIGH); //set 1G high
  digitalWrite (ADDRESS1A, LOW); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, HIGH);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
    //Byte 4
  digitalWrite (NOTG1, HIGH); //set 1G high
  digitalWrite (ADDRESS1A, HIGH); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, HIGH);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  //Byte 5
  digitalWrite (NOTG1, HIGH); //set 1G high
  digitalWrite (ADDRESS1A, LOW); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, HIGH);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  //Byte 6
  digitalWrite (NOTG1, HIGH); //set enables high
  digitalWrite (ADDRESS1A, HIGH); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, HIGH);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  }
  
  void LEDs_OFF()
  {
  //Set data all low for LEDs off
  digitalWrite (DATA0, LOW);
  digitalWrite (DATA1, LOW);
  digitalWrite (DATA2, LOW);
  digitalWrite (DATA3, LOW);
  digitalWrite (DATA4, LOW);
  digitalWrite (DATA5, LOW);
  digitalWrite (DATA6, LOW);
  digitalWrite (DATA7, LOW);

  //Set up the '139 for the address you want (latches 1-6)
  //Byte 1
  digitalWrite (NOTG1, HIGH); //set enables high
  digitalWrite (NOTG2, HIGH);
  digitalWrite (ADDRESS1A, LOW); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  digitalWrite (NOTG2, LOW);
  digitalWrite (NOTG2, HIGH); //data bits are now clocked into the '273.
  
  //Byte 2
  digitalWrite (NOTG1, HIGH); //set enables high
  digitalWrite (NOTG2, HIGH);
  digitalWrite (ADDRESS1A, HIGH); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  digitalWrite (NOTG2, LOW);
  digitalWrite (NOTG2, HIGH); //data bits are now clocked into the '273.

  //Byte 3
  digitalWrite (NOTG1, HIGH); //set enables high
  digitalWrite (NOTG2, HIGH);
  digitalWrite (ADDRESS1A, LOW); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, HIGH);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  digitalWrite (NOTG2, LOW);
  digitalWrite (NOTG2, HIGH); //data bits are now clocked into the '273.

  //Byte 4
  digitalWrite (NOTG1, HIGH); //set enables high
  digitalWrite (NOTG2, HIGH);
  digitalWrite (ADDRESS1A, HIGH); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, HIGH);
  digitalWrite (ADDRESS2A, LOW);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  digitalWrite (NOTG2, LOW);
  digitalWrite (NOTG2, HIGH); //data bits are now clocked into the '273.


  //Byte 5
  digitalWrite (NOTG1, HIGH); //set enables high
  digitalWrite (NOTG2, HIGH);
  digitalWrite (ADDRESS1A, LOW); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, HIGH);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  digitalWrite (NOTG2, LOW);
  digitalWrite (NOTG2, HIGH); //data bits are now clocked into the '273.


  //Byte 6
  digitalWrite (NOTG1, HIGH); //set enables high
  digitalWrite (NOTG2, HIGH);
  digitalWrite (ADDRESS1A, HIGH); //set 1A, 1B, 2A, 2B for the address you want to write to
  digitalWrite (ADDRESS1B, LOW);
  digitalWrite (ADDRESS2A, HIGH);
  digitalWrite (ADDRESS2B, LOW);
  // clock the '139 so the output goes low then high
  digitalWrite (NOTG1, LOW);
  digitalWrite (NOTG1, HIGH);
  digitalWrite (NOTG2, LOW);
  digitalWrite (NOTG2, HIGH); //data bits are now clocked into the '273.
  }

random(0,255) is the proper syntax not (0-255).

The comment is incorrect; that's not what that does.

Depending on the board, you may get a warning about it.

Spoiler
warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]

It's effectively a duplicate function declaration. Remove the int to make it a function call.

The following line is valid, but I did not know what to expect for the result of calling random() with a single negative argument, and the Arduino help page for random() does not enlighten.

newrnumber = random (0-255);

So I wrote this little program for the Arduino Uno R3, and am still puzzled. The printed result is 232, which, off the top of my head I can't explain. Anyone?

void setup() {
Serial.begin(115200);
while(!Serial);
Serial.println(random(-255));
}

void loop() {}

Just tried it with other negative values, and it appears that random() takes the absolute value of the supplied argument. Too lazy to look at the core code.

duck.ai says: "0xE8 likely indicates an error in the generating of a random number"... no source given

ESP32:

  if (howbig < 0) {
    return (random(0, -howbig));
  }

AVR:

  return random() % howbig;

which requires one to consider the difference between the remainder and modulo operations.

What are these lines intended to do?

I'm a little surprised this didn't cause a compile error. It would be more normal to write:

  String LOWSTRING = "LOW";
  String HIGHSTRING = "HIGH";

Or delete them, they are not used anywhere.

When you declare a variable inside a function, it doesn't exist and can't be used outside that function. You don't use this variable elsewhere in the function, so you can safely delete this code.

You think you are calling a function here but you are not. Putting "void" there means that the function is not actually called. It simply says that a function with that name will be defined later.

You should write:

  //Turn off all LEDs

  leds_off();

I don't think this will work. The idea here is to seed the pseudo-random number function with a genuinely random seed by reading an analog input pin which is not connected (floating). Reading such a pin will pick up random noise from the environment. But in your case, you are using pin A0 in your code as an output and setting it LOW, so using analogRead() on that pin will always give zero.

Same problem as before.

Using labels and goto should be avoided. I know you are only temporarily blocking further code execution for debugging purposes! But you could just use:

  while(true);

Here, you did not put a type in front of the function name, so you are correctly calling the function.

However, that function cannot see and cannot update the bit[] array you want it to update, because it exists only in loop().

Your genbitsofbyte() function declares it's own version of the array bit[] and updates that:

The array bit[] in loop() and the array bit[] in genbitsofbyte() are different arrays that happen to have the same name.

Maybe you should make the array bit[] a global variable.

You declared this function as type int, but you don't use the return statement to return any value. So you should declare the function as type void. As pointed out by others, giving the random() function a negative number as it's parameter won't give the result you are expecting.

Once you have corrected all these problems, if your code still doesn't work, post the complete updated version. But before you post it, click Tools->Auto Format in the IDE menu to correct the indentation. Indentation doesn't change the way the code works at all, but it makes it easier for us humans to read and spot more errors.

look this over

some output

  143 0x8F    1000 1111 
   81 0x51    0101 0001 
   87 0x57    0101 0111 
  205 0xCD    1100 1101 
   55 0x37    0011 0111 
   73 0x49    0100 1001 
  233 0xE9    1110 1001 
  113 0x71    0111 0001 
   52 0x34    0011 0100 
  141 0x8D    1000 1101 
   51 0x33    0011 0011 
   47 0x2F    0010 1111 
  172 0xAC    1010 1100

const byte Pins [] = { 3, 4, 5, 6, 7, 8, 9, 10 };

const int NOTG1     = A0;
const int ADDRESS1A = A1;
const int ADDRESS1B = A2;
const int NOTG2     = A3;
const int ADDRESS2A = A4;
const int ADDRESS2B = 11;

const int N = 8;
int bit [N];

char s [90];

// -----------------------------------------------------------------------------
int genNumber ()
{
    return  random (0,255);
}

// -----------------------------------------------------------------------------
void wrLs138 (
    int adr )
{
    digitalWrite (ADDRESS1A, adr & 1);
    digitalWrite (ADDRESS1B, adr & 2 >> 1);
    digitalWrite (ADDRESS2A, adr & 4 >> 2);
    digitalWrite (ADDRESS2B, adr & 8 >> 3);

    // clock the '139 so the output goes low then high
    digitalWrite (NOTG1, LOW);
    digitalWrite (NOTG1, HIGH); //data bits are now clocked into the '273.

    digitalWrite (NOTG2, LOW);
    digitalWrite (NOTG2, HIGH); //data bits are now clocked into the '273.
}

// -----------------------------------------------------------------------------
void wrLs138Bits ()
{
    for (int n = 0; n < N; n++)  {
        digitalWrite (Pins [n], bit [n]);
        wrLs138 (n);
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    int val = genNumber ();

    sprintf (s, "  %3d 0x%02X", val, val);
    Serial.print (s);

    Serial.print ("    ");
    for (int n = N-1; n >= 0; n--)  {
        bit [n] = (val & 0x80) >> 7;
        val <<= 1;

        Serial.print (bit [n]);
        if (! (n % 4))
            Serial.print (" ");
    }
    Serial.println ();

    wrLs138Bits ();

    delay (500);
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    for (int n = 0; n < N; n++)
        pinMode (Pins [n] ,OUTPUT);

    pinMode (NOTG1,     OUTPUT);
    pinMode (ADDRESS1A, OUTPUT);
    pinMode (ADDRESS1B, OUTPUT);
    pinMode (NOTG2,     OUTPUT);
    pinMode (ADDRESS2A, OUTPUT);
    pinMode (ADDRESS2B, OUTPUT);

    randomSeed (analogRead(0));
}

Does the following sketch help?

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A0));  //floaating volt: 0V t0 5V
}

void genRandom(byte n) {
  for (int i = 0; i < n; i++) {

    int randomNumber = random(0, 257);  //5numbers from 0 to < 257

    Serial.print("Number ");
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.println(randomNumber);
  }
}

void loop() {
  Serial.println();
  Serial.println("Generating 5 random numbers (0 to 256):");
  Serial.println("----------------------------------------");
  genRandom(5);  //five random number
  delay(1000);
}

Output:

Generating 5 random numbers (0 to 256):
----------------------------------------
Number 1: 71
Number 2: 130
Number 3: 98
Number 4: 31
Number 5: 192

Generating 5 random numbers (0 to 256):
----------------------------------------
Number 1: 115
Number 2: 248
Number 3: 20
Number 4: 130
Number 5: 20

In fact, to create seed, an integer number should be provided. If a fixed number (say: 3) is given, the random numbers will always be the same. To get randomly changing numbers a floating ADC-Ch0 may serve as a good seed. See post #11.

@nstobert

Your sketch of post #1 is modertaed as below and run to genertae five random numbers from 0 to 256 inclusive:

void setup() {
  randomSeed(analogRead(0));
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < 5; i++) {
    int rNum = genrnumber();
    Serial.print("newrnumber ");
    Serial.print(i); Serial.print(" :");
    Serial.println(rNum);
  }
  Serial.println("=====================================");
  delay(1000);
}

int genrnumber() {
  int newrnumber = random(0, 257);  //0 to 256 inclusive
  return newrnumber;
}

Output:

=====================================
newrnumber 0 :212
newrnumber 1 :97
newrnumber 2 :252
newrnumber 3 :201
newrnumber 4 :204
=====================================
newrnumber 0 :27
newrnumber 1 :29
newrnumber 2 :220
newrnumber 3 :26
newrnumber 4 :104
=====================================

But not in this case. See post #7

Thanks! (also to the poster of #7), I realized I used the wrong analog pin in the seed statement. Should be A5, that is truly unconnected in the circuit and never used in code other than reading it for the seed.

Thanks, this is very helpful!

Do you know the principle of the generation of random numbers? Can you generae random nubmers with decimal points? What is the use of random numbers? Can you give me five fractional numbers from 125.00 to 345.00 with seed 3.2?

Here is my code cleaned up, incorporating many of the things suggested. newrnumber comes back from the function that generates it just fine. However the bit[] variables generated in the genbitsofbyte function print in the function but not in loop. They are stored in the bit[] array so I don't understand why they come back as zero in the loop function as I defined the array before setup so it should be available to all functions, correct? Clearly I am missing something.

//Pin assignments

const int DATA0 = 3;
const int DATA1 = 4;
const int DATA2 = 5;
const int DATA3 = 6;
const int DATA4 = 7;
const int DATA5 = 8;
const int DATA6 = 9;
const int DATA7 = 10;
const int NOTG1 = A0;
const int ADDRESS1A = A1;
const int ADDRESS1B = A2;
const int NOTG2 = A3;
const int ADDRESS2A = A4;
const int ADDRESS2B = 11;

//Variables defined

String(LOWSTRING) = "LOW";
String(HIGHSTRING) = "HIGH";
int newrnumber = 0;
int x = 0;
int y = 0;
int z = 0;
int bit0 = 0;
int bit1 = 0;
int bit2 = 0;
int bit3 = 0;
int bit4 = 0;
int bit5 = 0;
int bit6 = 0;
int bit7 = 0;

byte led[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte bit[] = { 0, 0, 0, 0, 0, 0, 0, 0 };



//Setup/////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() {


  // initialize pins as output.
  pinMode(DATA0, OUTPUT);
  pinMode(DATA1, OUTPUT);
  pinMode(DATA2, OUTPUT);
  pinMode(DATA3, OUTPUT);
  pinMode(DATA4, OUTPUT);
  pinMode(DATA5, OUTPUT);
  pinMode(DATA6, OUTPUT);
  pinMode(DATA7, OUTPUT);
  pinMode(NOTG1, OUTPUT);
  pinMode(ADDRESS1A, OUTPUT);
  pinMode(ADDRESS1B, OUTPUT);
  pinMode(NOTG2, OUTPUT);
  pinMode(ADDRESS2A, OUTPUT);
  pinMode(ADDRESS2B, OUTPUT);

  //Set up LED array



  //Turn off all LEDs

  void leds_off();

  Serial.begin(9600);
}
//END SETUP ////////////////////////////////////////////////////////////////////////////////////////////////////

//Main loop/////////////////////////////////////////////////////////////////////////////////////////////////////

void loop()

{

  //Generate a random number

  //Call genrnumber function
  int newrnumber = genrnumber();
  Serial.print("newrnumber= ");
  Serial.println(newrnumber);

//This is just to stop execution so I can see what was printed

  genbitsofbyte(newrnumber);  //Call function that generates bit0 through bit7 variable values (1 or 0)
                              // to be used to turn LEDs on or off
  Serial.print ("In loop");
  Serial.print(bit[0]);
  Serial.print(bit[1]);
  Serial.print(bit[2]);
  Serial.print(bit[3]);
  Serial.print(bit[4]);
  Serial.print(bit[5]);
  Serial.print(bit[6]);
  Serial.println(bit[7]);

  delay(500);
}
//End of main loop /////////////////////////////////////////////////////////////////////////////////////////////

//Function to generate a random number (newrnumber)/////////////////////////////////////////////////////////////

int genrnumber() {

  randomSeed(analogRead(A5));
  newrnumber = random(0, 255);
  return newrnumber;
}

//Function to generates bit0 through bit7 variable values (1 or 0) to be used to turn LEDs on or off////////////

void genbitsofbyte(int newrnumber) {
  int bit[] = { 0, 0, 0, 0, 0, 0, 0, 0 };

  bit[0] = bitRead(newrnumber, 0);
  bit[1] = bitRead(newrnumber, 1);
  bit[2] = bitRead(newrnumber, 2);
  bit[3] = bitRead(newrnumber, 3);
  bit[4] = bitRead(newrnumber, 4);
  bit[5] = bitRead(newrnumber, 5);
  bit[6] = bitRead(newrnumber, 6);
  bit[7] = bitRead(newrnumber, 7);
  Serial.print (" in bit read function");
  Serial.print(bit[0]);
  Serial.print(bit[1]);
  Serial.print(bit[2]);
  Serial.print(bit[3]);
  Serial.print(bit[4]);
  Serial.print(bit[5]);
  Serial.print(bit[6]);
  Serial.println(bit[7]);
}

How about:

void genbitsofbyte(int newrnumber) {
  // see * int bit[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  Serial.print (" in bit read function");
  for (int i=0; i<8; i++) {
      bit[i] = bitRead(newrnumber, i);
      Serial.print(bit[i]);
  }
}

* This line creates a local variable named bit.
It will be destroyed as soon as you leave this function.
I added // so the compiler will not read this line anymore. That should solve your problem.
Read about scope and shadowing...

I also wrote a for loop to make your code more compact.

Set compiler warnings to all.
Your code has unused variables...