Procedure Random Number Logic

Started by ThePixelUnknown on Sat, 07/21/2018 - 02:21

Topic category: Help with MCreator software

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Procedure Random Number Logic
Sat, 07/21/2018 - 02:21

Is there a way to make a block that can pick from a random value scale (AKA a number from 1-3) with the procedure system. I did see that there was a random number option for the "π" math block, but the scale was stuck at 0-1 and I don't know how to change it. If anyone knows how to change it, please say so.

Thank you for responding in advance.

Last seen on 19:46, 28. Oct 2020
Joined Apr 2010
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
You can multiply random…
Sat, 07/21/2018 - 11:08

You can multiply random number by 3 to get numbers in the range from 0 to 3.

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I see. Well, thank you. I…
Sat, 07/21/2018 - 16:03

I see. Well, thank you. I would've never figured that out.

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
So, I tried doing this with…
Tue, 07/24/2018 - 01:29

So, I tried doing this with a block as I wanted it to randomly drop one item or another when you right-click it. I multiplied the random number by three and had three different outcomes, depending on what the random number would be. Well, this didn't work as nothing spawned from the chest. I'm sure I did the code correctly, but it seemed the random number didn't go through. Please help!

Last seen on 00:21, 11. Aug 2019
Joined Aug 2013
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Try to print the value so…
Tue, 07/24/2018 - 07:41

Try to print the value so you can actually see it.

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I was thinking about doing…
Tue, 07/24/2018 - 19:41

I was thinking about doing that, but I'm not sure how to print a variable into chat. Do you have a sample you could show, if it isn't too much trouble? Thanks in advance.

Last seen on 00:21, 11. Aug 2019
Joined Aug 2013
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
If you take the code…
Tue, 07/24/2018 - 20:29

If you take the code approach, it is System.out.println(STUFF_TO_PRINT);  (you will most likely want to save the generated value to a local variable and then use it to both print and the stuff you want), if you chose to use the procedure system, well , only I can tell you is that there deffinitely is some sort of print to console procedure block , but as I have not used the event/procedure system for like 3 years , I can not tell anything more exact now.

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Okay, thank you! I will try…
Wed, 07/25/2018 - 02:57

Okay, thank you! I will try this soon. Thank you for the support and sorry if it's a hassle for how long this is taking.

Last seen on 19:46, 28. Oct 2020
Joined Apr 2010
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
You can use the print block…
Wed, 07/25/2018 - 08:42

You can use the print block in the Text category of the procedure system.

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Okay, tried doing the block…
Wed, 07/25/2018 - 23:08

Okay, tried doing the block method. It did work, however, the random number had many decimals after it, which is probably why nothing would happen when I right-clicked the block since I was trying to compare the random number variable to specific whole numbers. I could use "<" or ">" signs to specify a range, but that would not work for three or more different outcomes.  Any way to convert these decimals to whole numbers so I could use them for whole-numbered outcomes? Thanks again, by the way

Last seen on 00:21, 11. Aug 2019
Joined Aug 2013
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
If you used equal condition…
Thu, 07/26/2018 - 00:55

If you used equal condition instead of the others, then yes - that is the reason, as I am like 98% sure that MCreator uses Math.random() for generating a random decimal in range 0 - 1 (both inclusive) , or if not this one , then either Random#nextFloat() or Random#nextDouble() (which both again generate decimal in the same range).
That is actually why multiplying the result by your desired maximal value gives you all the numbers between 0 and the value.
Example (the maximal value = 10)

  • 0.0*10 = 0
  • 1.0*10 = 10
  • 0.625*10 = 6.25
  • 0.97*10 = 9.7
  • etc...

Now, how can you solve this? First this sentence 

I could use "<" or ">" signs to specify a range, but that would not work for three or more different outcomes.

is totally wrong. The simplest (but not the only) way to use these is checking in reverse order, so for a scenario, with maximal value 3:

  • is X >= 2 --- do stuff , else continue to next check
  • is X >= 1 --- do stuff, else do some another stuff (or you can use another check X >= 0)

If you want to use the rounding approach, well you can, even though I think that it is the worst one in terms of performance. You have several options here, each one of them is a bit different:

  • Math.round() - round in a way you would expect - so less than 0.5 down, 0.5 or greater up
  • Math.floor() - always rounds down (hence the name then name "floor")
  • Math.ceil() - always rounds up (hence the name name "ceil")
  • int typecast (basically you add (int) before the value so you cast it to int) - same as Math.floor()

In case that you do not want to use the greater than or equal check approach and that you do not want (hopefully) to use the round approach, you can just generate the number as an int from start (and therefore as a non-decimal number) , by using Random#nextInt(). This function has an optional parameter of type int. Without the paramter the method returns a value of a whole number in range 0-1 (so 0 or 1) , if you pass some argument , it generates a number in a range 0 - X (exclusive) , so if I pas a number 4 , it will return a whole number in range 0-3.

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Okay, I did not about…
Thu, 07/26/2018 - 01:03

Okay, I did not about putting the "greater than or equal to" in reverse order. I believe that just solved my issue (hopefully!). Thank you for guys for sitting through this long stream of comments for a small question like this!

Last seen on 02:40, 27. Feb 2020
Joined Feb 2018
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Thank you guys*
Thu, 07/26/2018 - 01:03

Thank you guys*

Last seen on 21:10, 6. Apr 2020
Joined Apr 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I get 0-3 is there a way I…
Thu, 04/02/2020 - 16:39

I get 0-3 is there a way I can make it 1-3? getting 0 from my Blue Berry Bush doesn't make sense so help would be awesome.