Thread: perfect randomness?

  1. #1
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Exclamation perfect randomness?

    In my other post with the subject frequency? , the result comes out to be 52 for heads and 48 for tails. It means that it is not 100% randomized result. If had been correctly randomized the result should have been 50 for heads and 50 for tails, know what I mean? How can I achieve perfect randomness in this case?

    here is the code which I posted and later corrected.

    Code:
    #include<iostream>	/* include header files */
    #include<conio.h>	/*                      */
    #include<iomanip>	/*                      */
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::setw;
    
    void flip();	// prototype
    
    int main()
    {
    	flip();	// function call
    }
    
    void flip(void)
    {
    	int frequency1=0;	// declare & initialize frequency1 to 0
    	int frequency2=0;	// declare & initialize frequency2 to 0
    	int face;	// declare face
    
    	for(int toss=1; toss<=100; toss++)	// coin tossed 100 times
    	{
    		face=1+rand()%2;	// generate randomly 1 | 2
    	switch(face)	// switch face
    	{
    	case 1:
    		++frequency1;	// increment heads
    		break;
    	case 2:
    		++frequency2;	// increment tails
    		break;
    	default:
    		cout<< "It should never get here:";	// default should never occur
    		break;	// optional
    	}	// exit switch
    	}	// exit for loop
    	
    	cout<< setw(10) << "Face" << setw(13) << "Frequency" << endl;	// display results
    	cout<< setw(10) << "Heads" << setw(13) << frequency1 << endl;
    	cout<< setw(10) << "Tails" << setw(13) << frequency2 << endl;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why?

    No other coin tossing experiment will ever produce a 50:50 result every single time.

    Perhaps you should try a few real experiments yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    actually, if you flip a coin, you'll usually get something more like 33:66 result... try it yourself and you'll notice that you get heads/tails 6 or 7 times in a row... that's just teh way it is... if you do it 50 million times, it'll be closer to 50:50, but if you only do it about 50 times, you definatley won't get 50:50...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    This only applies if you keep going on in eternity, and of course when you calculate it on a paper but it wont apply in real life, only if you do it to eternity.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1) Your computer cannot generate random numbers. It can generate numbers that APPEAR to be random, but they do have a pattern to them. True random number generators are not built solely out of electronics -- for example, they may include radioactive sources (radioactive decay being a random event).

    2) It is only with an infinite number of flips that you get exactly 50/50 on a coin flip. 50% is the mean, and the expected value of any set of coin flips, but the means will have some deviation -- that is, you won't always get exactly 50/50 splits. For example, a 60/40 split is still fairly likely on 100 flips of a coin.
    Last edited by Cat; 01-18-2004 at 12:02 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    A coin toss isn't random, the force rotation and direction of force in addition to obscuritues, friction and distance to travel will result in the side that is face up, nothing is truly random, everything can be mathematically calulated.

    If you want an even ratio, just tell the program to stop when both frequencies are equal to each other rather than counting to 100.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> nothing is truly random, everything can be mathematically calulated.

    Many physical laws seem to be causal in nature. The underlying principles of quantum mechanics, however, appear to be quite random. And as of yet, there is no causal theory underlying either string theory or quantum theory.

    At any rate, the principle that satets that things will get closer to the mean as you perform more trials is the "Law of Large Numbers".

    A much "better" source of randomness would be, as Cat said, to use a more observably random source. One such source is the rate of radioactive decay. Another is thermal patterns on a relatively fixed object (though, this type of device would be easier to screw up by say, turning on a light in view of your camera). There was also a bit of work done with lava lamps.

    If you want a different deterministic pseudo-random number generator, search for algorithms, and you'll surely find many. Linear congruential generators are fairly common.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And as of yet, there is no causal theory underlying either string theory or quantum theory.
    Of course, seeing as how they are both theories...
    My best code is written with the delete key.

  9. #9
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Lightbulb

    okeys. It means that my result 52-48 is very much random.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Prelude
    >And as of yet, there is no causal theory underlying either string theory or quantum theory.
    Of course, seeing as how they are both theories...
    That was part of my point. At least that stuff looks random.

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    25
    Take a look at the "Mersenne Twister" itīs supposed to be "better" and faster than the original randomizer.
    http://www.math.keio.ac.jp/~matumoto/emt.html

  12. #12
    Registered User
    Join Date
    Jan 2004
    Posts
    25
    There is if you think about it... aka...

    Random Number: 35
    Then do
    RandomNumber - 8

    Now there's not much of a pattern going on. Only problem is if it goes negative, then ouch.
    My work:
    http://phil.webula.net

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    anyone who programs a lot with random numbers will tell you, rand() is definitely not the way to go. there are a lot of resources on random number generators. i have a data structures book that has an entire chapter devoted to generating random numbers.
    I came up with a cool phrase to put down here, but i forgot it...

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I program with random numbers pretty often... I haven't seen any problems with using rand() yet... as long as you seed it, it should be fine... unless there's some kind of portablitiy problem or something...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by Dark Nemesis
    nothing is truly random, everything can be mathematically calulated.
    345
    7
    19
    44
    .5
    56,023.3
    3.3

    That isn't truly random? I would love to see a pattern for that.
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My perfect number assignment...
    By Ninestar in forum C Programming
    Replies: 8
    Last Post: 11-16-2005, 02:47 AM
  2. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 05:11 PM
  3. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 11:09 AM
  4. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM
  5. perfect squares?
    By ahalya in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2001, 09:38 PM