# [SOLVED] Make a more complex dice (random number generator)?

0 favourites
• 14 posts
From the Asset Store
Let’s improve your match skill in a fun competition!
• Hi C2 community!

I'm trying to make a random number generator only using global variables and simple commands, but I'm not able to get anywhere.

The random generator draws 6 fixes numbers (1 to 6, like a dice), but always exclude the last two drawn numbers.

To do this I'm using three variables: current (to storage the current drawn number), var1 and var2 (to storage the mini-game and avoid selecting the same number).

There's the current CAPX with the generator test.

If anyone has a suggestion to make it work properly, I would be very grateful. <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy">

SOLVED!

Below there is a CAPX example based on R0J0hound suggestion:

There is also another example posted by korbaach, that uses array:

https://app.box.com/s/t4gvdfwdhvp75uijslgkdyznqiex7qag

• I dont really understand what you mean......but hhis could help: https://www.scirra.com/tutorials/1267/r ... rpg-tricks

Dont use the same purpose from this tutorial, just use the same technique.

Hope that help!

• I dont really understand what you mean......but hhis could help: https://www.scirra.com/tutorials/1267/r ... rpg-tricks

Dont use the same purpose from this tutorial, just use the same technique.

Hope that help!

Thanks, I'm using the same principles, but my case is a bit more complex.

To simplify my problem, I thought it best illustrate with numbers, because this is the basic principle adopted.

In my case, the random generator always need to exclude the last two drawn numbers.

• just a thought...

Raffle!.capx

• This could work also:

var cur=0

var last1=0

var last2=0

on click

--- set last2 to last1

--- set last1 to cur

------while

------ system compare: (cur=last1)|(cur=last2) = 1

--------- set cur to choose(1,2,3,4,5,6)

• just a thought...

Raffle!.capx

Thanks for this functional example, korbaach!

I was trying to do it without arrays (more 'cause I don't known yet to handle it), but I believe that this is only possible through the arrays, right?

• This could work also:

var cur=0

var last1=0

var last2=0

on click

--- set last2 to last1

--- set last1 to cur

------while

------ system compare: (cur=last1)|(cur=last2) = 1

--------- set cur to choose(1,2,3,4,5,6)

Thanks R0J0hound! I'll try it here! ^^

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• I don't know if proceeded correctly, R0J0hound, but the CAPX with your instructions not worked. <img src="{SMILIES_PATH}/icon_e_sad.gif" alt=":(" title="Sad">

Can you take a look at what's going on?

• The "system compare" needs to be in the same block as the "while". It won't work if it's a sub event.

• The "system compare" needs to be in the same block as the "while". It won't work if it's a sub event.

It's alive, R0J0hound! Thanks dude!

• lukezero just a thought...

try to avoid "While" loops..... whenever you can..

• Perhaps he could use a function to avoid "While" loops?

• "while" can be very useful to use. No reason to avoid it. You just need to make sure you're not causing an infinite loop by using it.

The method I proposed was to choose a random number and if it's either of the last two values it tries again. The only drawback is it could have the chance of looping a lot if one of the last values gets picked over and over again, but that is a slim chance in this case.

The method korbaach suggests is to have a list of all the numbers except the two last and choose one of them. Arrays could be used to do it or even text like the following.

number cur=0

number last1=0

number last2=0

text list=""

on click

--- set last2 to last1

--- set last1 to cur

--- set list to replace(replace("123456", str(last1), ""), str(last2), "")

--- set cur to int(mid(list,int(random(len(list))),1))

• "while" can be very useful to use. No reason to avoid it.

R0J0hound Of course, you're right,my fault...perhaps better advice was, be extremely careful when you use "While" loops

• 14 posts