PDA

View Full Version : Rand - not so random?


johnjoe
12-10-2008, 06:45 AM
Does anyone know if I can make the rand function more random?? :curious:

any and all ideas welcome

TIA

AndersEgleus
12-10-2008, 07:22 AM
In what way do you find it insufficiently random?

Anyway, one thing you could do is:

float $rand = rand ($veryLargeNegativeNumber, $veryLargePositiveNumber);
$rand = noise ($rand);

That should random it up some more for you I think, although it is of course more expensive. And you have to remap the output from -1 > 1 into the range you want.

johnjoe
12-10-2008, 07:56 PM
Thanks AndersEgleus :thumbsup: that's a great help :bowdown:

ewerybody
12-11-2008, 01:35 PM
But can you tell us whats the actual case that forces you to have that more random randomness? :curious:

shibumenon
01-20-2009, 04:50 PM
Anyway, one thing you could do is:

float $rand = rand ($veryLargeNegativeNumber, $veryLargePositiveNumber);
$rand = noise ($rand);

That should random it up some more for you I think, although it is of course more expensive. And you have to remap the output from -1 > 1 into the range you want.

However large you keep your numbers, its still going to give you the same random numbers.
Am not sure how noise would help, coz it would also return the same value everytime for a particular number.



In what way do you find it insufficiently random?

But can you tell us whats the actual case that forces you to have that more random randomness? :curious:

As long as I am the sole user of a script which uses rand, I could possibly come up with some workarounds. But if you are faced with a multiple user situation where, say you need to generate a random number, which becomes, say, the name of a folder, and you use this folder to dynamically save some file. Suppose this folder location is on the network, and all other users of the script will also be running the same random script. Thats when the random number suddenly doesn't sound too random ... btw, thats not an arbitrary situation, thats exactly the situation was in once..
Work arounds ... you could maybe use the date command to get a value like
2009/01/20 17:35:02. Then convert that into a valid integer by removing the '/'s and ':'s
20090120173502, maybe also write a script which juggles the digits in the returned value .. Then use this value somewhere within your rand generator ?

Another good way would be to use timerX() between 2 user inputs (2 mouse clicks maybe, or button clicks) , and calculate the time difference, and use that during random generation ...

Or else ditch Mel's not-so-usefull-random-generator and go for a python one ?

Dontnoun
01-21-2009, 04:19 PM
get random float by maya start time & menory page faults
global proc float TM_rand(){
//get random float by maya start time & menory page faults --------01.19.2009.Dontnoun
int $rand_range[] = {0,1}; //user define rand range
float $crnt_tim = `timerX` ; int $crnt_men_pf[] = `memory -pf`; // get two random number: maya start time & menory page faults

float $TM_rand_a = `sqrt $crnt_tim`; float $TM_rand_b = `sqrt $crnt_men_pf[0]`; float $TM_rand = $TM_rand_a*$TM_rand_b;
string $buf_rand[];
tokenize (string($TM_rand)) "." $buf_rand;
$TM_rand = $TM_rand - float($buf_rand[0]); //remove int part,only keep float
$TM_rand = `substring (string($TM_rand)) 3 (size(string($TM_rand)))`; //get more random number

string $mnl_sed = "seed(" + $TM_rand + ");"; eval $mnl_sed; //set random seed;
return (rand ($rand_range[0],$rand_range[1]) );
}TM_rand;

trancor
01-21-2009, 11:22 PM
Well, if your not familiar with it, the % symbol is extremely useful in a situation like this.

float $val = rand(-40000,40000) % 5;

will give you random values between -5 and 5 only.


float $val = rand(-5000,5000) % 5;

Will give you a different set of random values between -5 and 5 only.

and for a positive number only
float $val = rand(10000) % 5;

So set the rand out of some obscene number and just % it by the max number you want for the random value

Giap
01-21-2009, 11:39 PM
I also have a question about "rand" as well.


I want to make a cube move randomly in X axis from -5 to 5 so I type cube.translateX = rand (-5,5); But the result is that the cube pops randomly from -5 to 5 in X axis instead of moving. Could you help me write this expression?

Dontnoun
01-22-2009, 01:42 AM
problem with the mel rand commands is each time when you restart maya it returns the same "random" numbers!

from maya doc : An overview of MEL's multiple random number stream support:
The random numbers returned by rand, sphrand and gauss all follow a fixed sequence (called a random number stream). Prior to the addition of multiple random number streams to Maya, the only way to ensure repeatability was to explicitely reseed the generator using the seed function. There are, however, cases where random calls occur in arbitrary order and reseeding is either impossible or impractical.

To resolve this issue, MEL supports an arbitrary number of user-defined random number streams. Each MEL random call (rand, sphrand, gauss, etc) allows a stream name to be provided which identifies the random stream to be used during random number generation. Streams are created implicitely: each MEL random function checks first if the stream exists and if not, it is created and seeded with an internal default. The internal default is identical so that the default and user-defined streams always generate the same sequence of numbers when generated via an identical set of random calls. (This repeatability can be avoided by manually reseeding the stream via the seed function). Stream names may be any user-defined string not containing a "*" as a character. The stream named "default" is pre-defined and refers to the standard stream used by the non-stream-based MEL random calls. The intent with random number streams is that the user can target specific areas of MEL random calls to use dedicated random number streams and thus ensure repeatability of the random numbers which are generated.

Internally, streams are managed through an efficient database, so it is not unreasonable to allocate large stream counts (e.g. 100,000 streams). Streams may be deleted via the delrandstr function. The state of a random number stream may be queried via the randstate function.

trancor
01-27-2009, 01:45 AM
I also have a question about "rand" as well.
I want to make a cube move randomly in X axis from -5 to 5 so I type cube.translateX = rand (-5,5); But the result is that the cube pops randomly from -5 to 5 in X axis instead of moving. Could you help me write this expression?


Change "rand" to "noise"

noise(time)*5 would make it randomly and smoothly move between random values between -5 and 5

EightBit
02-20-2009, 06:07 AM
I'm using the rand function to generate an integer within a short range - say 1-7.
I've tried a few of the suggestions in this thread, but no matter which one I used, I noticed that when I convert a random number to an int, I get a lot of repetition, and I realized that the conversion to integer ALWAY ROUNDS DOWN, removing a significant amount of randomness. Is there some simple code that will round the random number up or down based on its % between integers?
Thanks.

Edit: Never mind, I found this:
http://www.keegan3d.com/tutorial_view.php?id=1

r4inm4ker
02-20-2009, 06:22 AM
I'm using the rand function to generate an integer within a short range - say 1-7.
I've tried a few of the suggestions in this thread, but no matter which one I used, I noticed that when I convert a random number to an int, I get a lot of repetition, and I realized that the conversion to integer ALWAY ROUNDS DOWN, removing a significant amount of randomness. Is there some simple code that will round the random number up or down based on its % between integers?
Thanks.

this code will round down or up the variable depending on the percentage..


{
float $random = 5.3;
int $newInt;
if($random - int($random) < 0.5)
$newInt = int($random);
else
$newInt = int($random)+1;
}

EightBit
02-21-2009, 07:54 AM
Thanks, that's a bit simpler than the code in the article that I linked to.

CGTalk Moderation
02-21-2009, 07:54 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.