4 Replies - 529 Views - Last Post: 19 May 2017 - 01:18 AM Rate Topic: -----

#1 thaus_15   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 69
  • Joined: 09-May 17

For Pseudo_random number generator :why we using "static" keyw

Posted 18 May 2017 - 10:46 PM

Sorry guys, For me one more doubt, in my program code why we using "static" keyword to declare as a variable in the program Code (for generating Random variable sequence). Please explain me in according to my program codes.

Instead of "static", shall i use "unsigned char" in my program code. What will be change in the code ?

Through Google search I found out " use of static keyword with a variable that is local to a function, it allows the last value of the variable to be preserved between successive calls to that function"
#include <stdio.h>

int *Randombits( ) {
   
   static int  result[33];
int bit;
int i=0;
static int reg = 0xACF5;  // starting value, generates different patterns with each call to Randombits
   
   
for (i=0;i<=32;i++)
 {
   
        bit  = ((reg >> 0) ^ (reg >> 2) ^ (reg >> 3) ^ (reg >> 5) ) & 1;
        reg =  (reg >> 1) | (bit << 15);
      result [i] = bit;
   
   }
   
   return result;
}

int main () {

   /* a pointer to an int */
   int *p;
   int i;

   p = Randombits();
	
   for ( i = 0; i < 30; i++ ) {
      printf("%d",  *(p + i) );
   }
 
   return 0;
}

This post has been edited by ndc85430: 18 May 2017 - 10:56 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: For Pseudo_random number generator :why we using "static" keyw

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,025
  • Joined: 13-June 14

Re: For Pseudo_random number generator :why we using "static" keyw

Posted 18 May 2017 - 10:59 PM

First of all, please use code tags when posting code here, as you're asked to do in the box where you type your posts. Doing so keeps the formatting and adds line numbering and syntax highlighting, all of which help everyone (you included) to read the code

View Postthaus_15, on 19 May 2017 - 06:46 AM, said:

Instead of "static", shall i use "unsigned char" in my program code. What will be change in the code ?


static is not a type, it's a keyword that does what you described in the latter part of your post. It allows you to keep a value in memory through successive calls to your function - it's basically a form of caching.
Was This Post Helpful? 0
  • +
  • -

#3 thaus_15   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 69
  • Joined: 09-May 17

Re: For Pseudo_random number generator :why we using "static" keyw

Posted 18 May 2017 - 11:04 PM

Sorry for wrong formatting. You telling that "static" keyword will keep a value in memory through successive calls to your function.

Through Google search I found, it allows the last value of the variable to be preserved between successive calls to that function". Then it will return only last value ?
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,025
  • Joined: 13-June 14

Re: For Pseudo_random number generator :why we using "static" keyw

Posted 18 May 2017 - 11:07 PM

Yes. Why don't you try writing a simple function with a static variable in it, where you change the value somewhere in the function and also print the value at the top of the function? That should help you see what's going on.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2467
  • View blog
  • Posts: 4,630
  • Joined: 30-May 10

Re: For Pseudo_random number generator :why we using "static" keyw

Posted 19 May 2017 - 01:18 AM

For reference -> https://cboard.cprog...ional-call.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1