Thread: Uniform sampling

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    56

    Uniform sampling

    Hi

    My goal is to build a C function that perform a uniform sampling. I mean that I have a big file with a lot of data and I'd like to take just some data samples accordly the uniform distribution.

    Es.
    file1: a b c d e f g h i l m n o p q

    An example would be to flip a coin for each letter and select all letters
    for which the coin showed the head.

    I have used the srand() system call, but it is not uniform.
    Can anyone suggest a way to start?
    Thanks

    D

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    rand() (not srand()) is uniform, but not necessarily on the range you want it to be on.

    But, in terms of drawing from a file, you need to choose every line, not necessarily 50-50, but in the ratio (number needed to still be drawn)/(number still in the file).

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    hi

    I'm reading about rand, so with:

    a=rand();

    I get a number uniformly distribuited between 0 and RAND_MAX

    Thanks for reply

    D

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Don't forget to seed rand() using srand() or something similar. Time is always a good bet for a seed. If you want to flip a coin, do this:

    Code:
    a = (int) (rand() / RAND_MAX + 0.5)
    This will either give a 0 or a 1, with equal probability.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dedalus View Post
    An example would be to flip a coin for each letter and select all letters for which the coin showed the head.
    To be clear, that is not uniform sampling. That is Poisson sampling.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    This will either give a 0 or a 1, with equal probability.
    No, that will give zero with 100% probability.

    You need to cast one of the division operands to get the result you are looking for:
    Code:
    a = (int) ((double)rand() / RAND_MAX + 0.5)

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Have you checked the FAQ. Prelude wrote a well written article on it here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uniform resize of a window
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 02-13-2008, 02:40 PM
  2. Replies: 6
    Last Post: 11-12-2005, 11:57 AM
  3. Sampling rate help
    By cbraddoss in forum C++ Programming
    Replies: 0
    Last Post: 09-16-2005, 09:31 AM
  4. Problem with multiply defined objects
    By Tostado in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2004, 05:05 PM
  5. Analog to Digital (conversion time)
    By Johnny5 in forum Tech Board
    Replies: 6
    Last Post: 12-13-2002, 02:10 PM

Tags for this Thread