Thread: Good ramdomize/distribution algorithm?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    47

    Good ramdomize/distribution algorithm?

    Hi, I got a problem.

    Lets say I got a list with 100 free spots.

    I want save something in 60 spots, and I want do distribute them as evenly as possible. That is I don't want spot 1-59.

    Anyone got any suggestions on how to solve this in a good way?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I'm sorry, I don't really understand what you mean by "That is I don't want stop 1-59".
    Can you please explain?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    r = rand() % 100;
    perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    Quote Originally Posted by Abda92 View Post
    I'm sorry, I don't really understand what you mean by "That is I don't want stop 1-59".
    Can you please explain?
    ok, lets say the list is 10 long

    That is I don't want place: 1,2,3,4, I want like: 1,3,5,7, I don't wan't clusters.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you talking about spreading the load over multiple CPU's here?

    If so, you probably need to KNOW what you are trying to achieve, and implement an algorithm for that. My suggestion will still work to spread it around, but there's absolutely no guarantee that you don't get 1, 2, 4, 7 as a sequence of 4.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, you want to choose 60 integers from 1 to 100 inclusive such that they are distributed as evenly as possible. One way is to choose all the odd numbers, then choose every 10th even number.

    EDIT:
    But if matsp's guess is correct, then obviously this choice is not a one time thing. Consequently, just choose at random, in the long run things will even out even if the first time you get 1-59.
    Last edited by laserlight; 11-07-2007 at 10:23 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by micke_b View Post
    Lets say I got a list with 100 free spots.
    The problem makes no sense. Lists don't have 'free spots'. Lists only contain exactly what items you put in them and are always accessed sequentially.
    Perhaps you mean an 'array'?

    Are you doing hashing, and want to distribute items into an array without clumping so as to reduce collisions or something?
    You really could be doing anything from your description, so we need WAAAY more info!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good Programming schools are they out there???
    By jbarby in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-12-2009, 06:31 PM
  2. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  3. Database Search Algorithm
    By Krupux in forum C Programming
    Replies: 1
    Last Post: 08-28-2003, 09:57 PM
  4. Algorithm question
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 10:52 AM
  5. string searching algorithm......help
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 09:59 AM