Page 1 of 1

How do I generate Family object without duplicate?

PostPosted: Wed Aug 19, 2015 2:50 pm
by mrneko
Hi everyone,

As title stated I have 45 objects in a family and I have to generate 30 of them randomly on the map. I don't want any of the 30 to repeat though.

I tried to follow the tutorial here => https://www.scirra.com/tutorials/315/ge ... duplicates but it's still rather confusing for me. For the part where randomNumber = floor(Random(0,15)) .. how should I change it?

Thanks.

Re: How do I generate Family object without duplicate?

PostPosted: Sat Aug 29, 2015 10:56 pm
by Borgi
Random(0,15) generates a number from 0 to up to 15, but not 15.
With floor, he makes this random number an integer.

You could use this, with Floor(Random(45)) -> this will generate an integer between 0 and 44.
Generate 30 of these.
After that, loop through the array, and base on what the value of the array -> create your object.
Generate different object based on all the random value could be in the array

Re: How do I generate Family object without duplicate?

PostPosted: Tue Sep 01, 2015 3:44 pm
by RamPackWobble

Re: How do I generate Family object without duplicate?

PostPosted: Fri Sep 04, 2015 11:56 am
by mrneko
@RamPackWobble

Thanks :)

Re: How do I generate Family object without duplicate?

PostPosted: Sun Dec 06, 2015 6:11 am
by bholtzman
I know this is a little late, by about 4 months or so, but I'm thinking that using a dictionary to store each random number as it's used would be a good way to check against what's still available in the random pool. Then only accept numbers that aren't already in the dictionary.