The RPG Maker Resource Kit

RMRK RPG Maker Creation => XP => Topic started by: kenth21v on July 25, 2011, 03:17:22 PM

Title: [XP] How to make random number w/c do not produce duplicate value?
Post by: kenth21v on July 25, 2011, 03:17:22 PM
I am trying to have a random set of questions.
i tried having a variable with a random value of 1-5.
and conditional branch of which the value of the variable is selected then the question is the same number.
E.G Control Variable[001: RandomNum] = Random No. (1...5)
Conditional Branch: Variable [001: RandomNum] == 1
 Text: Question number 1

Conditional Branch: Variable [001: RandomNum] == 2
 Text: Question number 2
and so on....

but my problem is, random number is sometimes duplicating like 1-2-2-3-2. so the questions are being repeated. i need it not being repeated.

hoping for your help guys. Thanks !
Title: Re: [XP] How to make random number w/c do not produce duplicate value?
Post by: modern algebra on July 25, 2011, 04:40:32 PM
Well, one way would be to use self-switches inside a loop, like this:

Loop
  Control Variable[001:RandomNum] = Random No. (1..5)
  Conditional Branch: Variable [001: RandomNum] == 1
    Conditional Branch: Self-Switch A is OFF
      Question Number 1
      Control Self-Switch: A = ON
      Break Loop
    Branch End
  Branch End
  Conditional Branch: Variable [001: RandomNum] == 2
    Conditional Branch: Self-Switch B is OFF
      Question Number 2
      Control Self-Switch: B = ON
      Break Loop
    Branch End
  Branch End
  ...
Repeat Above

It's a bit of a pain and you could only have four though (unless you use game_guy's More Self Switches (http://rmrk.net/index.php/topic,42808.0.html) script). You'd also need a way to break the loop immediately if no more questions were left.

You could also do the same, except using a variable instead of self-switches to track which questions have been answered. For the first question, you'd add 1 to the tracking variable. For the second question, you'd add 2; for the third question you'd add 4; for the fourth question you'd add 8; for the nth question you'd add 2^n-1, etc... Then you could know whether each question has been asked before by looking at that tracking variable.

However, I don't really see the point of randomizing the questions if you need to have them all asked anyway? It's a lot of work for a feature of questionable value.
 

Also, there might be an easier way to do it, but that's the first that came to my mind. I have a sunburn though, so I'm not really thinking clearly.
Title: Re: [XP] How to make random number w/c do not produce duplicate value?
Post by: kenth21v on July 27, 2011, 07:28:47 AM
Thanks for your reply.. this would be a great help. i think i'll just substittue the self-switch with a variable.  ;D
Title: Re: [XP] How to make random number w/c do not produce duplicate value?
Post by: Zeriab on July 27, 2011, 07:44:40 PM
Well, one way would be to use self-switches inside a loop, like this:

Loop
  Control Variable[001:RandomNum] = Random No. (1..5)
  Conditional Branch: Variable [001: RandomNum] == 1
    Conditional Branch: Self-Switch A is OFF
      Question Number 1
      Control Self-Switch: A = ON
      Break Loop
    Branch End
  Branch End
  Conditional Branch: Variable [001: RandomNum] == 2
    Conditional Branch: Self-Switch B is OFF
      Question Number 2
      Control Self-Switch: B = ON
      Break Loop
    Branch End
  Branch End
  ...
Repeat Above

I suggest you do it this way. It's simply way too dangerous. First of all who knows how many rounds it can take, secondly and most importantly the loop becomes an infinite loop the 6th time it's called. You can clean it up and protect it, but having some code which will freeze the game if you miss a route into the loop a 6th time is not nice at all.

Luckily only some slight alteration are needed to make the loop safer.

Control Variable[001:RandomNum] = Random No. (1..5)
Loop
  Conditional Branch: Variable [001: RandomNum] <= 1
    Conditional Branch: Self-Switch A is OFF
      Question Number 1
      Control Self-Switch: A = ON
      Break Loop
    Branch End
  Branch End
  Conditional Branch: Variable [001: RandomNum] <= 2
    Conditional Branch: Self-Switch B is OFF
      Question Number 2
      Control Self-Switch: B = ON
      Break Loop
    Branch End
  Branch End

  ...

  Conditional Branch: Variable [001: RandomNum] <= 5
    Conditional Branch: Self-Switch B is OFF
      Question Number 5
      Control Self-Switch: B = ON
      Break Loop
    Branch End
  Branch End
  Control Variable[001:RandomNum] -= 1
  Conditional Branch: Variable [001: RandomNum] <= 0
    Break Loop
  Branch End
Repeat Above


Personally I would use an extra variable to keep track of the number of questions left so I wouldn't need to use a loop at all, but practically I doubt there are any significant difference with only 5 questions.

*hugs*
Title: Re: [XP] How to make random number w/c do not produce duplicate value?
Post by: kenth21v on July 28, 2011, 02:12:03 AM
here is what i did:
Loop
Control Variables: [0010: RandomQues] = Random No. (1...20) --->>> for 20 sets of questions
 Conditional Branch: Variable [0010: RandomQues] == 1
   Conditional Branch: Variable [0026: q1] == 0
      Text: QUESTION
      Control Variables: [0011: NuumberofQuestion] += 1
      Control Variables: [0026: q1] += 1
    Else
    Conditional Branch: Variable [0011: NumberofQuestion] == 5
       Break Loop
     Branch End
   Branch End
 Branch End
Conditional Branch: Variable [0010: RandomQues] == 2
..... and so on....
Conditional Branch: Variable [0011: NumberofQuestion] == 5
       Break Loop
Branch End
Repeat Above

it works fine,
so thanks to everybody!  ;D