M
M
maxemga2020-12-25 19:00:20
C++ / C#
maxemga, 2020-12-25 19:00:20

How to make a ramdom object spawn without repeating?

I have four rectangles of different colors and I need them to spawn ramdomly on four given points
. How to make them ramdomly spawn on points, I know, but how to make them spawn without repeating, I don’t know
. That is, the red cube spawned and it is larger can't spawn on the other 3 positions and these 4 positions need to have one object of each color

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2020-12-26
@maxemga

Colors in the list (List) and mix. Then take it out one by one.

private static Random rng = new Random();  

public static void Shuffle<T>(this IList<T> list)  
{  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rng.Next(n + 1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}

there is still one on Linq, but I haven't checked it
var shuffled = myList.OrderBy(x => Guid.NewGuid()).ToList();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question