Code:
var list = ds_list_create();
for( var i = 1; i <= 50; i++){
ds_list_add(list,i);
}
ds_list_shuffle(list);
list is a ds_list with all numbers in random order.
This is basically what Caffeine said to do. It's comparable to my second approach, but it does not create a list of unique "random numbers", it's a list of incremented numbers in a random order.
Mine is almost the same, except it is a list of semi-random numbers. It's not 100% random like my first method because the amount of numbers in a specific range is limited by the scale jumped each time. Mine is more likely to favor the lower divisions of the number range, but like I said in my first post, each algorithm has cons and pros. The simplicity of yours can't be beaten, but in order to get the actual value, and not the order of numbers random, there will be more complexity if he needs that.
///random_set(max,items,min)
var r, n=irandom((argument[0] - 1) div argument[1]);
r[0] = irandom(argument[0]);
for(var i=1; i<6; i++)
r[ i ] = (r[i-1] + irandom(n) + 1) mod argument[0];
if argument_count==2
for(i=0; i<6; i++)
if r[ i ] < argument[2]
r[ i ] = argument[2];
return r;
I think you meant to use
argument_count == 3, because you're accessing the third argument when using argument[2]. Also, yours is very interesting. Unlike strawbry's where the order is what's random and not the numbers, your numbers are partially randomized but the sequence is semi-predictable. The reason I say this is with yours, when you get to a more limiting range, the wave is much more noticeable. And I think in a situation where it requires random unique numbers, the order is important at this point. Compared to my second algorithm, it's close in approach style, but is replacing the shuffle with an offset to the wave making it more predictable. (Although I love the elegance of your approach).
@TheouAegis I put it into a jsFiddle to test with, tell me if I implemented it wrong.
https://jsfiddle.net/Lemony_Andrew/yz0dv30v/
@flyingsaucerinvasion
This would be fore generating random numbers that aren't necessarily part of a sequence. you could use a map first to eliminate any possibility of duplicate numbers. Maps cannot contain more than one key that has the same value. You could continue adding keys to the map until the map has reached a certain size. While you've been adding keys to the map, whenever one is successfully added, you add it to a list or queue so they can be quickly read when needed.
This is essentially my first algorithm, except far, far slower and requires many more steps. The extra-creative way of using maps is very interesting, though. This seems like an approach I wouldn't mind using in JavaScript, but with a few added optimizations.