Click here to Skip to main content
15,921,295 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello,

New to this site and C# all together.

I was wondering how I could build a amateur version of a Magic 8 ball.
All I need help on is this: How do I generate a random yes and no answer?

The user would input a random question and well I want the application to give a random yes and no.

Thank you very much for the help!

-G
Posted
Updated 25-Nov-10 3:22am
v2
Comments
Dalek Dave 25-Nov-10 9:23am    
Edited for Readability.

Random rnd = new Random();

if((rnd.Next() & 1) == 0)
{
    // Yes ...
}
else
{
   // No
}


This works by checking if the lowest bit is not set (integer is even) but you could also check if bit is set ( == 1 which amounts to odd).

Another way to go is % (modulo operator) which gives the remainder.
x % n gives the remainder of the division of x by n: e.g X = 6 N = 3
6 % 3 == 0

This is very usefull if you need more than the 2 options "yes" and "no".
For example:

0 == "Yes"
1 == "No"
2 == "Ask again later"
3 == "Not very likely"

int numberOfChoices = 4;
Random rnd = new Random();
switch(rnd % numberOfChoices)
{
    case 0:
        //"Yes" stuff goes here
        break;
    case 1:
        //"No" stuff goes here
        break;
    case 2:
        //"Ask again later" stuff goes here
        break;
    case 3:
        //"Not very likely" stuff goes here
        break;

}



Cheers

Manfred
 
Share this answer
 
v2
Comments
fjdiewornncalwe 25-Nov-10 8:31am    
Nice implementation. I did some stuff that required "random" True/False cases one time, but I never even thought of the solution you have suggested. I like it, and much simpler than the one I used. (Mind you, that was many years ago...)
Manfred Rudolf Bihy 25-Nov-10 8:37am    
Thanks!
Dalek Dave 25-Nov-10 9:23am    
Good Answer, and more or less as anyone else would have suggested, but with more options!
Toli Cuturicu 25-Nov-10 12:55pm    
Your answer is almost correct (you got my 5). But, for the sake of absolute correctness, I have to say that your 8-ball machine is slightly biesed into giving "yes" answers. In fact, it will answer "yes" in 50,00000001164153% of cases... And I mean it, it is no joke. Anyway, not in a casino.
Manfred Rudolf Bihy 26-Nov-10 3:10am    
@Toli: Would your care to tell me the sample size you used? (I do assume you employed an empirical method and not something analytical, like taking .NET Random function apart :) )
Google is your friend

You should always start with a simple search on google because most beginner questions will already have answers out there.

Cheers
 
Share this answer
 
MIDL
Random rand = new Random();
int i = rand.Next(2); // generates numbers between 0 and 1

string strYesOrNo = (i == 0) ? "yes" : "no";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900