# random number generation?

I have 10 positions to fill.
[] [] [] [] [] [] [] [] [] []

I have 7 different items to fill those positions.
1 2 3 4 5 6 7

some items must be used 2 times, items 1 and 3.
All others need only be used once.

Now, I want to generate a random number to place the 7 items in the 10 positions.

How would I do that with the logic bricks? I have thought about this a lot, but am stumped at the point of setting the random number to generate a new number without using one that is already used?

I thought I could set it to run from 1-10 until a property is set to stop it. Then check each number against 10 properties to see if it is there, if it is, check them again to see if it is used twice. If it is to be used twice then set a prop with that number again, and then set another prop to not use that number again.

Then go through it all again. It just seems a long way around?

Thank you.:spin:

with python you could use the function :

``````
random.sample()

``````

look at python.org help to see more.
I think you need to get some additionnals libs to use it !

Python really is the way to go with this one.
I would recommend the following, as it’s what I use most of the time.

``````import random
rand = random.randint(0, 10)
``````

This will set the variable ‘rand’ between 0 and 10.

GreatOne7? and only 7 posts? Curious.

Thanks I will look at that. I had hoped to not use python for this little game, but it would probably make it easier in the long run. And I could use a matrix then, which the bricks dont have.

Any more ideas? Suggestions?

try this little tutorial…
I make a 6 sided dice for the gibs.
http://mmph.phpnet.us/tutorial8/index.html
all logic brick goodness.

Thanks looking at it now.

Umm… I believe the numbers should be 1-8, cause if it’s only 1-7 and 2&3 are used twice that only makes a total of 9 numbers.

basically all you have to do is this

``````
import random
pos = [1, 2, 2, 3, 3, 4, 5, 6, 7, 8]
random.shuffle(pos)

``````

Viola, those 10 positions are filled randomly with the numbers you need.

Jason Lin

I guess I cant count, lol. So maybe I have 8 instead of 7.

So to use that script, how would I ensure the 2 of them would be used twice?

Thanks very much, thats a cool function.

MMPH!: I got stuck in one place, when I first read it, and tonight am downloading the file. Hoping I understand that better.

Will ask if I cannot figure it out…I dont remember the specifics now, lol,long day…something about triggering the thing to start…

@Goldentaji… that’s sweet… I didn’t know there was a python shuffle function… that shuffles lists

oh, duh! LOL… set it to 10, shuffle all of them, then place them based on the number… See I am awake now!

Mmph! I understand it. It is a little bit different, because I need to track the positions used and the pieces used to fill those positions.

Its to randomize the beginning of the game piece’s location each time the game is started.

That random script function looks like it would be the easiest, but I have to figure out how to apply it. I can read python script and write a little, but I havent studied it at all.

Thanks for you help.