Click to See Complete Forum and Search --> : Don't have a coin on you? No problem!


vee-eye
12-07-2001, 01:18 AM
I know the feeling. You need to make a major financial decision, but you just can't find a coin to decide. That's why I wrote this Java program:


public class Coin
{
public static void main(String[] args)
{
int side = 0;
side = (int)(Math.random()*2); // Random integer from 0 to 1

if(side == 0)
System.out.println("Heads!"); // Either it's heads (0)
else
System.out.println("Tails!"); // Or tails (1)
}
}


Save this in a file called Coin.java
Then run the following on the command line in the same directory as the file:
javac Coin.java
java Coin

Run java Coin as many times as you want. ;) Hopefully you have the tools installed.

YaRness
12-07-2001, 12:40 PM
do you need to seed the rand function in java? or is that dependent on the OS?

Dru Lee Parsec
12-07-2001, 12:42 PM
Short, Sweet, Compiles perfectly, Runs the first time.

SHIP IT!

Nice little program. :)

Dru Lee Parsec
12-07-2001, 12:49 PM
public class Coin {
public static void main(String[] args) {
int side = 0;
side = (int)(Math.random() * 2);

if (side == 0)
System.out.println("Heads!");
else
System.out.println("Tails!");


System.out.println("\n\nNow we do the loop test");
int heads = 0;
int tails = 0;
for (int i = 0; i < 100000 ;i++ ) {
side = (int)(Math.random() * 2);
if (side == 0){
heads++;
}
else {
tails++;
}
}
System.out.println("\n\nTotal heads vs tails");
System.out.println("Heads : "+ heads);
System.out.println("Tails : "+ tails);
}
}



Just something fun to play with this morning.

Thanks Vee-Eye.

[ 07 December 2001: Message edited by: Dru Lee Parsec ]

Gnu/Vince
12-07-2001, 02:47 PM
#!/usr/bin/env ruby

side = rand(2)

if side == 0 then
puts "Heads!"
else
puts "Tail!"
end

jcrowe
12-07-2001, 03:28 PM
#!/usr/bin/env python
import random
side = random.randint(1,2)
if side == 1:
print 'Heads'
elif side == 2:
print 'Tails'

inkedmn
12-07-2001, 03:46 PM
#!/usr/bin/env python

import random

coin = random.randrange (0, 2, 1)
if coin == 0:
print "Heads! w00t!"
else:
print "Tails! w00t!"

vee-eye
12-07-2001, 04:09 PM
Originally posted by Dru Lee Parsec:

System.out.println("\n\nNow we do the loop test");
int heads = 0;
int tails = 0;
for (int i = 0; i < 100000 ;i++ ) {
side = (int)(Math.random() * 2);
if (side == 0){
heads++;
}
else {
tails++;
}
}
System.out.println("\n\nTotal heads vs tails");
System.out.println("Heads : "+ heads);
System.out.println("Tails : "+ tails);


That's very creative! When I was doing the Chi-square test in my research class (which involved flipping a coin like 50 times), I could have used that.

Note: I'm not up to the part in my book where it talks about loops. ;)

And I thank the rest of you for showing how this could be done in other languages. :)

Originally posted by YaRness
<STRONG>do you need to seed the rand function in java? or is that dependent on the OS?</STRONG>

What?

[ 07 December 2001: Message edited by: vee-eye ]

jkm
12-07-2001, 05:31 PM
int main(void)
{
srand(time(0));
return printf("%s\n", (rand() % 2) ? "heads" : "tails");
}

Whipping Boy
12-07-2001, 06:04 PM
#!/usr/bin/env coin
flip(22);
show result;

Dru Lee Parsec
12-07-2001, 06:26 PM
Apparently, when I edited my last post I deleted the part before the code. What it SHOULD have said was:

***************************************

do you need to seed the rand function in java? or is that dependent on the OS?

No, the rand function in Java is seeded from the system clock. This will cause a new psuedo random sequence to be created every time the program is run.

I added a loop to see just how random Vee-Eye's code is and it looks good. What follow is some code that demonstrates this :

[ and then there was the code ]

******************************

I'm not sure why it got deleted.

Vee-Eye said What? to the "seed" question so I'll explain a bit further.

No computer generated sequence is truely random, even though it can pass a chi aquare test. This is because the randome sequence is just an alogrithm that creates a sequence and it has to start somewhere. If the initial variables all have exactly the same value then the same "random" sequence will be created.

So what programmers do is that they take something that is essentially random and instert it into their algorithm. Usually it's the system clock time in milliseconds. Every time somebody uses the program it's a different time so the number of milliseconds since whenever the clock count started will always be different.

Hope it helps.

vee-eye
12-07-2001, 06:39 PM
Thanks for the explanation Dru, but what exactly was YaRness asking? The two sentences in his question seem to contradict each other.

To be perfectly honest, I always had a vague feeling that random numbers were generated by the current time (system clock). How else could the first random number method be generated?

Whipping Boy
12-07-2001, 06:49 PM
I see no one likes the custom language I wrote for this thread whose sole purpose is to flip a coin and show how many heads and how many tails.

And yes, I did actually write an interpreter for that language :)

Bradmont5
12-07-2001, 07:32 PM
$ if [ $RANDOM%2 -eq 1 ]; then echo "Heads"; else echo "Tails"; fi

Dru Lee Parsec
12-07-2001, 08:04 PM
Thanks for the explanation Dru, but what exactly was YaRness asking? The two sentences in his question seem to contradict each other.

I interpreted it to mean "Do all OS's set the randomness by the system clock or do some OS's require the programmer to get the time and seed the Random method itself?"

For example, you used the static random method in the Math class in your program. But if you look at the Random class you see that it has a constructor that looks like this:

public Random(long seed)

If you "seed" the constructor with the same number and then use it in your program you'll see that you always get the exact same sequence of heads or tails. And the weird thing is that the sequence itself is random enough to pass a chi-square test. But if it's absolutly repeatable then it's certainly not random. Hence the term "psuedo-random" when discussing software randomness.

YaRness
12-07-2001, 10:20 PM
Originally posted by Dru Lee Parsec:
<STRONG>If you "seed" the constructor with the same number and then use it in your program you'll see that you always get the exact same sequence of heads or tails.</STRONG>i think that's what i was thinking of.

George Kilroy
12-08-2001, 08:22 PM
#!/usr/bin/env python
import random
side = random.randint(1,2)
if side == 1:
print 'Heads'
elif side == 2:
print 'Tails'
#!/usr/bin/env python
import randomcoin = random.randrange (0, 2, 1)
if coin == 0:
print "Heads! w00t!"
else:
print "Tails! w00t!"
None of those worked for me. What is wrong with these two examples or could I be missing some modules? I'd really like to get random to work.

[ 08 December 2001: Message edited by: George Kilroy ]

[ 08 December 2001: Message edited by: George Kilroy ]

Super Bakemono
12-08-2001, 09:15 PM
#include &lt;stdio.h&gt;

int main(int argc, char **argv) {
printf("Heads!\n");
return 0;
}

Gnu/Vince
12-08-2001, 10:17 PM
Originally posted by George Kilroy:

#!/usr/bin/env python
import random
side = random.randint(1,2)
if side == 1:
print 'Heads'
elif side == 2:
print 'Tails'
#!/usr/bin/env python
import randomcoin = random.randrange (0, 2, 1)
if coin == 0:
print "Heads! w00t!"
else:
print "Tails! w00t!"
<STRONG>
None of those worked for me. What is wrong with these two examples or could I be missing some modules? I'd really like to get random to work.
</STRONG>

Did you try mine in Ruby? iT works.

George Kilroy
12-08-2001, 10:36 PM
I don't have Ruby, I've got it on disk and will install it. I want to get the random thing to work because I want to use random on my own Python projects.

vee-eye
12-09-2001, 01:12 AM
Originally posted by George Kilroy:
<STRONG>I don't have Ruby, I've got it on disk and will install it. I want to get the random thing to work because I want to use random on my own Python projects.</STRONG>

Maybe this could help you:

Python.org documentation for generating psuedo-random numbers (http://www.python.org/doc/current/lib/module-random.html)

JBrian
12-09-2001, 04:47 AM
I thought about writing this earlier this week... Kinda like tossing a coin, except with a little more variety.

#include &lt;iostream.h&gt;
#include &lt;time.h&gt;
#include &lt;stdlib.h&gt;
/*** The Almighty Magic 8 Ball **********
* *
* compile with "g++ 8ball.cpp -o 8ball"*
* and place in your path. *
***************************************/
int main() {
srand( (unsigned)time(NULL) );
int ans = (rand() % 22) + 1;
switch (ans) {
case 1:
cout &lt;&lt; "Signs point to yes.\n";
break;
case 2:
cout &lt;&lt; "Yes.\n";
break;
case 3:
cout &lt;&lt; "Reply hazy, try again.\n";
break;
case 4:
cout &lt;&lt; "Without a doubt.\n";
break;
case 5:
cout &lt;&lt; "My sources say no.\n";
break;
case 6:
cout &lt;&lt; "As I see it, yes.\n";
break;
case 7:
cout &lt;&lt; "You may rely on it.\n";
break;
case 8:
cout &lt;&lt; "Concentrate and ask again.\n";
break;
case 9:
cout &lt;&lt; "Outlook not so good.\n";
break;
case 10:
cout &lt;&lt; "It is decidedly so.\n";
break;
case 11:
cout &lt;&lt; "Better not tell you now.\n";
break;
case 12:
cout &lt;&lt; "Very doubtful.\n";
break;
case 13:
cout &lt;&lt; "Yes - definitely.\n";
break;
case 14:
cout &lt;&lt; "It is certain.\n";
break;

case 15:
cout &lt;&lt; "Yes - definitely.\n";
break;
case 16:
cout &lt;&lt; "It is certain.\n";
break;
case 17:
cout &lt;&lt; "Cannot predict now.\n";
break;
case 18:
cout &lt;&lt; "Most likely.\n";
break;
case 19:
cout &lt;&lt; "Ask again later.\n";
break;
case 20:
cout &lt;&lt; "My reply is no.\n";
break;
case 21:
cout &lt;&lt; "Outlook good.\n";
break;
case 22:
cout &lt;&lt; "Don't count on it.\n";
break;
exit(0);
}

}


[edit] ugh, the forum hosed my code

[ 09 December 2001: Message edited by: JBrian ]

Niminator
12-09-2001, 05:43 AM
I wonder what a statistics professor would think of using pseudo-random numbers in place of a coin toss to determine randomness of a coin toss.

MrNewbie
12-09-2001, 08:48 AM
Originally posted by Whipping Boy:
<STRONG>
And yes, I did actually write an interpreter for that language :)</STRONG>

Can you post it?

inkedmn
12-09-2001, 04:01 PM
Originally posted by George Kilroy:
#!/usr/bin/env python
import random
coin = random.randrange (0, 2, 1)
if coin == 0:
print "Heads! w00t!"
else:
print "Tails! w00t!"[/code]
None of those worked for me. What is wrong with these two examples or could I be missing some modules? I'd really like to get random to work.

[ 08 December 2001: Message edited by: George Kilroy ]

[ 08 December 2001: Message edited by: George Kilroy ]


you had the import line in the wrong spot.
:D