Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to quickly shuffle the numbers 1 through 4?

Tags:

c#

random

shuffle

Looking to shuffle four variables (trying to change the order they appear in on a multiple choice list).

I've been poking around for a while but I can't quite get my head around the logic, and looking up random shuffles in past questions gives super-detailed algorithms that are beyond my newbie skills (and the needs of this program I'm trying to write, I'd just like to make a multiple-choice image picker).

Ideally I'd like something that follows this pseudocode:

// int Answer1 = Random(min1 max4)

// int Answer2 = Random(min1 max4)

// int Answer3 = Random(min1 max4)

// int Answer4 = Random(min1 max4)

// If Answer 1 equals ANY of the other three, re-randomize Answer1 and loop.

// Loop through this cycle for all answers.

I'd post my current regular code, but frankly, it's garbage. :( This seems like a simple enough problem but I just can't get it right.

Thanks in advance!

like image 469
user236494 Avatar asked Jan 06 '10 05:01

user236494


People also ask

What is the shuffle algorithm for a list of 400 numbers?

Now list is a shuffling of {1, 2, 3, 4}. The algorithm that I used here is the Fisher-Yates shuffle. Show activity on this post. Well, technically, who cares if it's just 4 numbers of 400 numbers. You should be using an implementation of Fisher-Yates shuffle. However, to make it easier to understand:

How to shuffle digit order in a number?

Quickly shuffle number's digits in your browser. To get your shuffled digits, just enter one or more numbers in the input field and this utility will randomize digit order in every number. Created by developers from team Browserling . Love math as much as we do? Then you'll also love our new project – MATHURLS – a super neat math news aggregator.

How to shuffle cells in Excel by random number?

Let’s try to do something similar for this task. First, insert a column before A and fill each cell with =RAND (). This will generate a random number between 0 and 1 in each of the new cells. Now, follow these steps: Select all of the cells that we want to shuffle (including the new cells we added) Click on Home -> Custom Sort…

What is the digit shuffle utility?

This is an online browser-based utility for randomizing the digit order in numbers. It randomly changes the positions of all digits until they're completely shuffled. We have an additional option that lets you preserve partial order of two or more digits. You can shuffle groups of digits rather than every individual digit.


2 Answers

Shuffling - http://www.codinghorror.com/blog/archives/001008.html

Though, don't use a guid, use a random number:

//create only once, please
static readonly Random random = new Random();

and then:

var numbers = Enumerable.Range(1, 4);
var shuffle = numbers.OrderBy(a => random.NextDouble());
like image 87
Kobi Avatar answered Sep 27 '22 19:09

Kobi


I like this extension method:

static class IListExtensions {
    public static void Shuffle<T>(this IList<T> list, Random rg) {
        for (int i = list.Count; i > 1; i--) {
            int k = rg.Next(i);
            T temp = list[k];
            list[k] = list[i - 1];
            list[i - 1] = temp;
        }
    }
}

Then:

Random rg = new Random();
List<int> list = Enumerable.Range(1, 4).ToList();
list.Shuffle(rg);

Now list is a shuffling of {1, 2, 3, 4}.

The algorithm that I used here is the Fisher-Yates shuffle.

like image 20
jason Avatar answered Sep 27 '22 18:09

jason