Hello everyone! I'm searching for a Random Binary Sequence which creates the
outcome of an XOR gate. I need to create a sequence of 2^(7) -1 bits....using my
C compiler. Thank you all for your time...
5 6785 Banfa 9,065
Expert Mod 8TB
Sorry ut that doesn't make sense to me, since XOR is definately not random I fail to see how a Random Binary Sequence could be the output of XOR.
Perhaps you could explain the problem in more detail.
Thanx for your interest. I need to create a Random Binary Sequence starting with
four '1' (bits), that is to say 1 1 1 1. We now assume that the two last bits are inputs of an XOR gate. We calculate their outcome which is 0. The next sequence we receive is 0 1 1 1, which results from the movement of 0 into the first position and the movement of the three first bits by one position to the right, just like if we had a shift register. We are doing this procedure many times and at last we receive the last column. I mean that each time we need to keep only the last of the four
bits. Now, if we need to receive 2^(7)-1 bits we must calculate 127 times the outcome of the XOR gate and receive each time the last bit, that is to say
127 bits.
Hope not to ask too much!!!! I think that such a function exists on Basic, but I
need to do it in C. Thanks again!!! :-)
The following code is a terrible way to do this, using only XOR for the booleans. There's a much nicer way to do it, I'll leave it up to you to figure out what the code is doing, and how to make it nicer. - int main()
-
{
-
bool input[] = {true,true,true,true};
-
int i;
-
-
for(i = 0; i < 127; i++)
-
{
-
input[0] ^= input[1];
-
input[1] ^= input[0];
-
input[0] ^= input[1];
-
input[2] ^= input[3];
-
input[3] ^= input[2];
-
input[0] ^= input[2];
-
input[2] ^= input[0];
-
input[0] ^= input[2];
-
-
if(input[0])
-
cout << "1";
-
else
-
cout << "0";
-
}
-
cout << endl;
-
system("PAUSE");
-
return EXIT_SUCCESS;
-
}
Ignoring the spaces, the result is: 000100110101111 repeated eight and a half times. I think chaotic is a better word than random.
Banfa 9,065
Expert Mod 8TB
OK well I think something like this is what you want -
#include "stdio.h"
-
-
-
int main(int argc, char* argv[])
-
{
-
unsigned char data = 0xff;
-
unsigned char result;
-
int ix;
-
-
for(ix=0;ix<127;ix++)
-
{
-
result = (data ^ ((data & 0x40) << 1)) & 0x80;
-
-
if ( result & 0x80 )
-
{
-
putchar('1');
-
}
-
else
-
{
-
putchar('0');
-
}
-
-
data = (data >> 1) | result;
-
}
-
-
putchar('\n');
-
-
return 0;
-
}
-
But the results are not very random
01101101101101101101101101101101101101101101101101 10110110110110110110110110110110110110110110110110 110110110110110110110110110
As you can see.
Banfa 9,065
Expert Mod 8TB
Ignoring the spaces, the result is: 000100110101111 repeated eight and a half times. I think chaotic is a better word than random.
Actually chaotic is a really poor word for it,a chaotic system never repeats which you admit yours does.
Still trying to work out what your code actualy does though :D
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thomas Mlynarczyk |
last post by:
I remember there is a programming language where you can initialize the
random number generator, so that it can - if you want - give you the exactly
same sequence of random numbers every time you...
|
by: Sonoman |
last post by:
Hi all:
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I...
|
by: Joe |
last post by:
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.
I...
|
by: JNY |
last post by:
I am using random to generate random numbers, thus:
int x,y;
for (y = 0;y < 5;y++)
{
x = random(50);
cout << x;
}
|
by: steflhermitte |
last post by:
Dear cpp-ians,
I want to apply a stratified sampling on an image. Following simplified
example will explain my problem.
The original image I with nrows and ncols is now a vector V of length...
|
by: Johannes Veerkamp |
last post by:
hi there,
i'm a newbie in c and i'd like to write a programm which generates random
numbers from 1 to 10 (integers). can anybody help me out with the correct
code?
thanx
|
by: fieldfallow |
last post by:
Hello all,
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
Assuming there isn't, as it appears from the docs that I have, is there
a...
|
by: pereges |
last post by:
I need to generate two uniform random numbers between 0 and 1 in C ?
How to do it ?
I looked into rand function where you need to #define RAND_MAX as 1
but will this rand function give me ...
|
by: bilgekhan |
last post by:
What is the correct method for generating 2 independent random numbers?
They will be compared whether they are equal.
What about this method:
srand(time(0));
int r1 = rand();
srand(rand());...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |