Thread: Ranom numbers having uniform distribution

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Even if rand() gives uniformly distributed numbers (which it does) that does not imply that you will get a flat histogram, nor should you expect it to, nor should you desire it to be so.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think the only way to do that would be a rand() which guaranteed no repetition until all possibilities have occurred, which is not exactly random. When you roll a die, there is some chance of rolling 3 sixes in a row. If you rolled 100 times, I suppose the distribution would be reasonable uniform, but for this reason you cannot ask it to be perfect -- that's not random.

    Going back to my earlier idea, you could put all numbers into an array, shuffle it using rand(0), and then pop them off one at time from this "random list"; when you reach the end, shuffle and start again. No matter how many times you do that, you will end up with a dead even number of repetitions for each value, but the order in which they occur will be random.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. Replies: 5
    Last Post: 10-12-2001, 03:51 AM