Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This homework is an approach to generating random numbers. In this technique a s

ID: 3692035 • Letter: T

Question

This homework is an approach to generating random numbers. In this technique a seed value is used to construct a new (seemingly random) value in the following manner: The seed, s, is written down in n-bit binary. Several bit positions, called taps, are used to generate a feedback bit. Bit n-1 is always one of the taps. The feedback bit, f is the exclusive-or of the tap bit values. The seed is shifted to the left one bit, dropping the leftmost bit. The rightmost bit is set to f. In this manner, a seed is transformed into a new (hopefully different!) value which may be used for whatever purpose is desired. This value can also serve as the next seed. If the taps are picked appropriately, a large number of unique values (ideally 2n -1 ) are generated before the original seed reappears. (This technique has one obvious deficiency that will not concern us: a seed of 0 will generate 0 and lock up the mechanism.) For example, using a nibble: User inputs seed of 6= 0110 User enters the taps 3 and 2. (These are the bit numbers, remember that BIT n-1 is always used so bit 3 would be used even if the user did not enter it.) 0110 the feedback is bit3 xor bit2, f = 0 xor 1= 1 Shift the bits to the left dropping the leftmost bit and the rightmost bit is set to f, which equals one. 1101=13. The random number is now 13. If we looped with 13 now being the seed. 1 xor 1 = 0 1010=10. 10 would be the next random number. Write a program,hw5.c 1. Use a command line argument for an unsigned integer for the seed and validate that this is a positive integer. End program if not a valid number. (Remember that it is a string and you must convert it to an integer.) 2. Prompt the user for the taps, end with a -1. Validate that the taps (bit numbers) are between 0 and 31. End the program if invalid. 3. Prompt the user for the number of random numbers that they would like to generate. Must be a positive integer greater than 0. If invalid ask user again to enter the number again. 4. Output the random numbers generated to a file “random.txt.”

Explanation / Answer

ANS;

#include <stdio.h> // header files
#include<conio.h>
int main() // main method program execution starts here
{

int a[20]; // arrat to store binary digits
int s,i=0,n,r,c,f,shift; // variables for the operations

printf("Enter input seed: "); // prompting user to enter value seed
scanf("%d",s);
while(s>0) // converting decimal to binary array
{
a[i]=s%2;
i++;
s=s/2;
c=i;
}
printf("Binary number of %d is = ",s); // printing binary number

for(int j=i-1;j>=0;j--)
{
printf("%d",a[j]);
}
printf(" ");
printf("Enter taps "); // promptng user for taps
label1: // validation lable loops on wrong input
scanf("%d",&tap1);   
scanf("%d",&tap2);
if(tap1<0 || tap1>31) // validating the taps
{
printf("tap must be in 0 to 31 enter taps again ");
goto lable1:
}
if(tap2<0 || tap2>31)
{
printf("taps must be in 0 to 31 enter taps again ");
goto lable1:
}
lable2:
printf("Enter number of random numbers to generate: "); // prompting user to enter number of random numbers to generate
scanf("%d",&r);
if(r<0) // validating input weather positive or not
{
printf("Enter positive integer ");
goto lable2;
}
for(int i=0;i<r;i++) // loop for the random number generations
{
f=a[tap1]^a[tap2];
shift=s<<1;
int arry[100]=binary(shift);

array[0]= f;

for(int t=0;t<100;t++)

printf("%d",arry[t]);

printf(" ");
  
}
  
}

int * binary(int decimalNumber) function to convert number to binary in operations for shifting purpose
{
long int decimalNumber,remainder,quotient; // required values for the operations

int binaryNumber[100],i=1,j;

quotient = decimalNumber;


while(quotient!=0){ // while loop for the binary array

binaryNumber[i++]= quotient % 2;

quotient = quotient / 2;

}
return binaryNumber; // returning the arrray consists binary digits
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote