 |
| C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 24th, 2004, 10:17 AM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
creating finate sequences
hi,
can someone please tell me how i can get my computer to create a finate sequence of coin tosses.
i want to tell it p(heads)=1/3
p(tails)=2/3
n=10
and it to give me a sequence eg
H, T, T, H,H,T,H,T,T,T.
How do i do this?
cheers
|

August 24th, 2004, 11:16 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The function rand() supplies a number between 0 and RAND_MAX (where RAND_MAX is supplied in <stdlib.h>, and is different for different compiler implementations). The function rand() is supposed to supply uniformly distributed values. You can do various statistical tests to see if it is adequate for your purposes.
So the following supplies a number between 0 and 1. I have left out most of the code, but here's what you need:
Code:
#include <stdlib.h>
...
double rand_value;
...
rand_value = (double)rand()/RAND_MAX;
...
Now, if you want p(heads) = 1/3, you get random numbers from the above code snippet, and if the value is less than 1/3, you assign 'H', otherwise assign 'T' to the current coin toss.
Does this help?
Dave
|

August 24th, 2004, 12:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I have some problem with your question,
its better you give the numbers assure you to get correct result.
for example its better you give the p(head)=x/n x=0..n then you can design better algorithms for getting exact results
I think the method Dave mentioned is very excellent but be careful maybe all
the results turn out only 'H' or 'T'.
--------------------------------------------
Mehdi.:)
|

August 24th, 2004, 01:21 PM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, if rand() gives uniformly distributed values, then the formula that I used will give values that are <1/3 one third of the time. Assigning 'H' to values < 1/3 is a way of simulating a coin toss with p(heads) = 1/3.
Code:
char toss_value;
if (rand()/RAND_MAX) < 1.0/3.0 {
toss_value = 'H';
}
else {
toss_value = 'T';
}
Why would you have a problem with this?
Dave
|

August 25th, 2004, 01:19 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Dave as I told you maybe again all the results turn out 'T' or 'H'
(I mean all the random numbers turn out grater than 1/3 then in output we have only 'T')
IMO,this question has some problem.
in the example original poster told us we cant have 1/3 for p(head) at all.
I cant understand this here!!!the question needs more explanations.
--------------------------------------------
Mehdi.:)
|

August 25th, 2004, 08:37 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
now I try correct this question ,suppose this condition
n=3 and p(head)=1/3
and in output we have
(1==H,0==T)
1 0 0
0 1 0
0 0 1
this program shows all the situations (non-repeated permutations) could occur
Code:
//Check for different numbers
int check(int *a,int f,int k,int n)
{
for(int i=f;i<k;i++)
{
if (a[i]==a[k]) return 1;
}
return 0;
}
//Generate all the permutations of a[k]..a[n-1]
//a is an array and n is the lenght of the array
//k is the statrting index for generating permutations
void p(int *a,int k,int n)
{
if(k==n-1)
{
for(int i=0;i<n;i++)
{
Console::Write(a[i]);
}
Console::WriteLine();
}
else
{
for(int i=k;i<n;i++)
{
//if this value differs others generate permutations
if(!check(a,k,i,n))
{
int temp=a[k];a[k]=a[i];a[i]=temp;
p(a,k+1,n);
temp=a[k];a[k]=a[i];a[i]=temp;
}
}
}
}
int _tmain()
{
int a[4];
a[0]=1;//optional,two times H and two times T
a[1]=0;
a[2]=0;
a[3]=1;
p(a,0,4);
Console::Read();
return 0;
}
if you cant get somewhere tell me I will explain it more exact how I wrote it.
--------------------------------------------
Mehdi.:)
|

August 25th, 2004, 08:45 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
From the original post:
Quote:
quote:
i want to tell it p(heads)=1/3
p(tails)=2/3
|
That is, the coin is not balanced (since for a "fair" coin and a "fair" toss, we would expect p(heads) to be 1/2.)
The function rand() is designed to give numbers with a uniform distribution between 0 and RAND_MAX.
If this is true, then the numbers obtained from the expression
x = rand()/RAND_MAX
are uniformly distributed between 0 and 1.
So for any integer N>0, let p = 1/N, then the probability that x is less than p is p. (So for, example, the probability that x is less than 1/3 is 1/3 and the probability that x is greater than or equal to 1/3 is 2/3). Therefore, if we assign the value 'H' whenever the number is < 1/3, and 'T' otherwise, we have simulated the coin toss as the Original Post requested.
Of course, if we run this experiment for, say 12 tosses, we may not get exactly 4 'H' and 8 'T', but for large numbers of tosses, we expect to have 'H' about 1/3 of the time. That's what is meant by the expression "probability of 'H' is 1/3".
Dave
|

August 25th, 2004, 09:16 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
quote:
That is, the coin is not balanced (since for a "fair" coin and a "fair" toss, we would expect p(heads) to be 1/2.)
|
no this is not the concept.we dont want the p(head),we want
P(The number of our coin turns out head in our erxperiments) surely the p(head)=1/2 all the time...
also I confirm your algorithm but what about if we want exact answers?
meanwhile consider a circumstance we want all situations not only one situation I think its better not to work with rand() in our algorithm
we should work with permutations for getting exact answers
in my opinion it could be a better idea.
(take a look at my previous post)
(I dont know again where is the original poster & what happend to him!)
--------------------------------------------
Mehdi.:)
|

August 25th, 2004, 09:56 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, he said:
Quote:
quote:
can someone please tell me how i can get my computer to create a finate sequence of coin tosses.
|
and
Quote:
quote:
i want to tell it p(heads)=1/3
p(tails)=2/3
|
To me, that means that we are telling the program that the probability of heads is 1/3, and we want to simulate a number of coin tosses.
If we want to know the probability that a fair coin gives the exact sequence that he gave (H, T, T, H, H, T, H, T, T, T), then we don't need a computer program, we know the answer is 1/2 to the power 10.
If we want to know the probability that a fair coin gives exactly four heads out of any 10 tosses (like his example sequence), we apply what we know about permutations; we don't need a computer program to generate a sequence of numbers.
The original post said he wanted to generate a finite sequence of coin tosses and he wants to tell it p(heads). I claim that my scheme does this.
Since the original poster has not responded, I can't know any more about his problem, and it's senseless for us to argue about what we think he meant.
Best regards,
Dave
|

August 26th, 2004, 12:53 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
i.e
p(HTHHT)=1/2^5
p(3 times H and 2 times T)=[5!/(3!*2!)]*1/2^5==p(heads:3/5)
I think no need for answering the original poster again,every possible way with different concepts was mentioned here.
--------------------------------------------
Mehdi.:)
|
|
 |