Hi all,
I'm a huge noob on this, so I'm sorry if this has been asked many times over. I'm a mature student, so didn't do coding etc. when I was at school - so again, apologies if this is obvious to you. I have tried the forum search function, but it seems that rand isn't often used here. I'm using the following code:
proc generateVariedCubes()
{
int $i;
int $j;
float $randSize;
for ($i=0;$i<5;$i++)
{
for ($j=0;$j<5;$j++)
{
$randSize = rand(2);
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
}
generateVariedCubes();
And this is working fine. What I'm trying to do is to take these results and remove any cube smaller than 1, so making the spot it was on empty. How can I filter my results, without removing the position completely?
Alternatively/also, I want to just have the cubes set to on or off, rather than sized from 0-2 - so it could function as a quick and dirty random map generator tool. I'm certain that such tools exist - but I'm trying to learn how to do it myself.
Ideally I would want the finished model in either UE or Unity. Thank you for any help you can give me!
Solved! Go to Solution.
Hi all,
I'm a huge noob on this, so I'm sorry if this has been asked many times over. I'm a mature student, so didn't do coding etc. when I was at school - so again, apologies if this is obvious to you. I have tried the forum search function, but it seems that rand isn't often used here. I'm using the following code:
proc generateVariedCubes()
{
int $i;
int $j;
float $randSize;
for ($i=0;$i<5;$i++)
{
for ($j=0;$j<5;$j++)
{
$randSize = rand(2);
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
}
generateVariedCubes();
And this is working fine. What I'm trying to do is to take these results and remove any cube smaller than 1, so making the spot it was on empty. How can I filter my results, without removing the position completely?
Alternatively/also, I want to just have the cubes set to on or off, rather than sized from 0-2 - so it could function as a quick and dirty random map generator tool. I'm certain that such tools exist - but I'm trying to learn how to do it myself.
Ideally I would want the finished model in either UE or Unity. Thank you for any help you can give me!
Solved! Go to Solution.
Solved by mspeer. Go to Solution.
Solved by mspeer. Go to Solution.
Hi,
if you just want random numbers greater than 1 you could use:
$randSize = rand(1) + 1;
Hi,
if you just want random numbers greater than 1 you could use:
$randSize = rand(1) + 1;
Hi Roland,
thanks for the answer. Currently I have something like:
21231
23121
12213
32112
11321
(rounded up).
What I want is
02200
20020
02202
20200
02202
I believe that your code would make every cube a minimum of size '1' - is this correct?
Hi Roland,
thanks for the answer. Currently I have something like:
21231
23121
12213
32112
11321
(rounded up).
What I want is
02200
20020
02202
20200
02202
I believe that your code would make every cube a minimum of size '1' - is this correct?
Your numbers seem to be missing a decimal point?
You can offset and "stretch" random numbers.
rand(1) = numbers greater 0 and less than 1
rand(1) + 1 = numbers greater 1 and less than 2
rand(1) * 10 + 1 = numbers greater 1 and less than 11
Your numbers seem to be missing a decimal point?
You can offset and "stretch" random numbers.
rand(1) = numbers greater 0 and less than 1
rand(1) + 1 = numbers greater 1 and less than 2
rand(1) * 10 + 1 = numbers greater 1 and less than 11
Hi!
I agree with @Roland.Reyer that it's better to alreay create the right values instead of removing unwanted values.
However if you also want to create a random placement based on a grid as in your example, then a simple if-statement can do this., where you can limit the min or the min and max size of cubes that should be created.
if ($randSize > 1)
or
if ($randSize > 1 && $randSize < 10)
Example with your MEL script (you can set any value for min and max, in this example a max value of 10 has of course no effect):
proc generateVariedCubes()
{
int $i;
int $j;
float $randSize;
for ($i=0;$i<5;$i++)
{
for ($j=0;$j<5;$j++)
{
$randSize = rand(2);
print ($randSize + " ");
if ($randSize > 1 && $randSize < 10)
{
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
print "\n";
}
}
generateVariedCubes();
Hi!
I agree with @Roland.Reyer that it's better to alreay create the right values instead of removing unwanted values.
However if you also want to create a random placement based on a grid as in your example, then a simple if-statement can do this., where you can limit the min or the min and max size of cubes that should be created.
if ($randSize > 1)
or
if ($randSize > 1 && $randSize < 10)
Example with your MEL script (you can set any value for min and max, in this example a max value of 10 has of course no effect):
proc generateVariedCubes()
{
int $i;
int $j;
float $randSize;
for ($i=0;$i<5;$i++)
{
for ($j=0;$j<5;$j++)
{
$randSize = rand(2);
print ($randSize + " ");
if ($randSize > 1 && $randSize < 10)
{
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
print "\n";
}
}
generateVariedCubes();
Apologies - I was using numbers to show an example grid.
If you read the 0 as an empty space, and the numbers as a cube.
Apologies - I was using numbers to show an example grid.
If you read the 0 as an empty space, and the numbers as a cube.
Well, then @mspeer has the solution with the "if" condition for the creation and position of the cube.
Alternatively you can also work with probability for the creation of the cubes, independently of their sizes:
// this would be true in ~50% of all cases
if ( rand(1) > 0.5 ) ...
BTW: if you play with random values, maybe you want to have a look at my "Randomizer":
Well, then @mspeer has the solution with the "if" condition for the creation and position of the cube.
Alternatively you can also work with probability for the creation of the cubes, independently of their sizes:
// this would be true in ~50% of all cases
if ( rand(1) > 0.5 ) ...
BTW: if you play with random values, maybe you want to have a look at my "Randomizer":
Hi!
In my last example I still have some print lines included that I used for testing,
it should have been only this:
proc generateVariedCubes()
{
int $i;
int $j;
float $randSize;
for ($i=0;$i<5;$i++)
{
for ($j=0;$j<5;$j++)
{
$randSize = rand(2);
if ($randSize > 1 && $randSize < 10)
{
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
}
}
generateVariedCubes();
Hi!
In my last example I still have some print lines included that I used for testing,
it should have been only this:
proc generateVariedCubes()
{
int $i;
int $j;
float $randSize;
for ($i=0;$i<5;$i++)
{
for ($j=0;$j<5;$j++)
{
$randSize = rand(2);
if ($randSize > 1 && $randSize < 10)
{
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
}
}
generateVariedCubes();
Can't find what you're looking for? Ask the community or share your knowledge.