Algorithm to generate an evenly distributed random permutation [closed] - algorithm

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have a set of N questions, each one categorized as one of R subjects. I'm trying to generate some random permutation of the questions for a quiz.
How would I generate a random permutation of the set of questions such that no two consecutive questions have the same subject? It does not need to be perfect; I just don't want to bore people with five of the same type of question in a row.
Additionally, if such an ordering is not possible (as in, 18 of A and 2 of B), could the algorithm find an "evenly distributed" permutation? (in this case, something reasonably like 6As, B, 7As, B, 5As)
Alternatively, since I'm delivering the questions sequentially, could I each time randomly select a remaining question of a different subject without the chance of forcing some repeats at the end?
I've looked around randomly on Google for a while and can't seem to find anything that fits this case. Other than randomly generating permutations until you find one that works, which is slow, ugly and stupid.

Here's an idea:
Group your questions into R groups according to their subject.
Shuffle each group separately
Interleave the groups depending on their relative size, so that they are evenly spaced.
This is simple and fits your requirements. A bit of 'randomness' is lost because the interleaving is always the same (i.e. question from subject 1, then from subject 2, then from subject 3, and start with subject 1 again).
This could be improved by randomizing the order in which you pick groups for each iteration.

Remove the previously used subject from the list of candidate subjects.
For example, in Java, suppose you have 10 subjects:
Subject previous = listSubjects.get( 0 );
while( true ){
listCandidateSubjects.remove( previous );
int xSelection = Random.nextInt( 10 );
Subject current = listCandidateSubjects.get( xSelection );
listCandidateSubjects.add( previous );
// generate question for current subject here
// when have enough questions break
previous = current;
}

Related

Which Data Structure/Algorithm concept does Facebook use for Friend Suggestions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Facebook suggests people we might know.
It shows us people we have looked up for.
And people who look up our profile.
I am curious to know which Algorithm does Facebook use for achieving this. Also the Data Structure that might help for this purpose.
I would rather answer on how this could be achieved.
Consider a Graph where Nodes are people and you have different kinds of vertices:
v1: The people N is friends with (undirected)
v2: The people whose wall were visited by N (directed, weighted by visit number)
v3: The people whose shared articles were seen by N (directed, weighted by article number)
v4: The people who commented/liked/shared on N's any activity (directed, weighted by a formula based on the number of actions of different kinds)
v5: The people mentioned in a post/comment of N (directed)
Indirect paths should be taken into account (friend of a friend), N is a Node. Based on the attributes of the graph the likelyness of the person is known can be computed by a formula, which can be defined in infinitely many ways. Also, one person might know another one, who forgot about the first one. Also, v(i) list is far from complete, it is meant only as an illustration.

What kind of algorithm is well suited for optimizing seat allocation in opera house? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So i have an opera houses with 10 rows and 10 columns of seats (Total :100). Each seat is allocated a preference value Aij. The preference value is halved if the group do not get seats in same row. Eg: If a reservation in opera house is for 5 people and only 2 can be accommodated in top row and 3 in next row, the preference value is actually halved for each seat. There are total of n reservations with 'n' > 100 seats. What will the best way to maximize the customer preference (n *Aij).If it can be done by linear programming, how should the equation look like.
I think this is not trivial to model as a MIP.
I gave it a try using a main binary variable x(i,j,g,r) where (i,j) is the row and column of the seat assigned to group g. The set r is {singlerow,multiplerows} to indicate whether the group is sitting in the same row.
Not sure if this is a particular good formulation, but it seems to work. Below are the equations I used:
I assumed that there are a number of groups g that each have a size. A group gets all seats it needs or it is not getting any seats.
I suspect that a constraint programming approach could be easier.
Conclusion: it can be done using a MIP formulation but it is somewhat cumbersome.

I am at a crossroads in my program and I was wondering which path would be more efficient time wise [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I currently see two ways to code the next step of my program and there are probably more, but the two routes I have are as follows.
I take the factors of the lowest number and loop through the other numbers two see if they share those common factors.
I find the factors of the lowest number and add it to a list. I then find the factors of the other numbers that do not exceed the lowest and add them to the same list. I then run through the list to check which is the highest number that appears x times.
I am leaning towards 1, but I'm not sure.
Sorry if this is too ambiguous, thanks.
Well, given the ambiguity, as stated: the 1st requires less steps and avoids the allocation of a data structure.

Expanding an arthimetic algorithm for a Weightlifting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I am a quite novice programmer, I have been teaching myself Ruby for a couple of weeks now, and I made a program that estimates your 1 repetition max possible weight lifting capabilities.
The algorithm I used is:
weight = gets.to_i
reps = gets.to_i
x=Rational(reps,30)
x=x.to_f
one_RM = weight*(1+x)
Now this has worked well to get an estimate equal to other 1rep max calculators out there, but what I want to do is to make it so that it takes any weight and reps value and lets the user choose which rep range to convert to.
If that was unclear here is an example of what I mean:
user writes 100 kg and 10 reps program prompts the user for a rep value it would like to get an estimate for, eg instead of only 1rm it can predict anything from 1-100rm etc.
here is a series of formulas for how to calculate the 1 rep maximum:
http://en.wikipedia.org/wiki/One-repetition_maximum#Calculating_1RM
I tried looking trough them and see if I could come up with an idea, but I'm not an expert at math(understatement) and I am very new to programming, so my brain is not contributing any useful solutions, any insights greatly appreciated!
You need to compute the 1RM and then invert the formulas you link to using the new value if r (the number of repetitions your user wants to do).
So with the formula you're using, and r_asked and w_asked the values for repetitions and weight :
You now need to do :
w_asked = one_RM / (1+ r_asked/30)

Why does SecureRandom.uuid create a unique string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why does SecureRandom.uuid create a unique string?
SecureRandom.uuid
# => "35cb4e30-54e1-49f9-b5ce-4134799eb2c0"
The string that method SecureRandom.uuid creates is never repeated?
The string is not in fact guaranteed unique. There is a very small but finite chance of a collision.
However in practice you will never see two ids generated using this mechanism that are the same, because the probability is so low.
You may safely treat a call to SecureRandom.uuid as generating a globally unique string in code that needs to manage many billions of database entities.
Here is a small table of collision probabilities.
Opinion: If I were to pick an arbitrary limit where you might start to see one or two collisions across the entire dataset with a realistic probability I would go for around 10**16 - assuming you create a million ids per second in your system, then it would take 30 years to reach that size. Even then, the probability of seeing any collisions over the whole 30 years of the project, would be roughly 1 in 100000.

Resources