Jump to content

Getting a random positive/negative value


anselm Hexicola
 Share

You are about to reply to a thread that has been inactive for 3342 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

As part of of an (already unwieldy) script I am writing, I want to flip numeric values randomly between + and -.

Now, I came up with some lines that seem to do what I want.

 

integer sign;
float arbitrary_float= 0.5;

determine_boolean()
{
	float rand1=llFrand(arbitrary_float);
	float rand2=llFrand(arbitrary_float);

	if (rand1>rand2)
		sign=TRUE;
		
	else if (rand1<rand2)
		sign=FALSE;
	
	else
		determine_boolean();
}
	
default
{
	touch_start(integer total_number)
	{
		determine_boolean();
		llOwnerSay("sign is "+(string)sign);
	}
}

 

1. As I say, it  seems to work for me.

2. But, is there a problem with the final "Else"? I only added that to cover the eventuality that both rand1 and rand2 outputted identically.

3. How would this be best done? - ie, generate a random TRUE or FALSE.

Link to comment
Share on other sites


anselm Hexicola wrote:

 

2. But, is there a problem with the final "Else"? I only added that to cover the eventuality that both rand1 and rand2 outputted identically.

3. How would this be best done? - ie, generate a random TRUE or FALSE.

the problem with 2. is that bc llFrand is uniformly distributed then is possible that the function as wrote wont ever return. Is potential that it can always gen the same numbers and the script will go catatonic

from programmer/scripter pov catatonic also incl. cycles of 2 or more to get the result you want when can get result in 1 cycle. Is best to avoid coding this way

for 3. if do just want TRUE | FALSE then

integer sign = (integer)llFrand(2);  // 0 false. 1 true

 eta: cast

 

 

Link to comment
Share on other sites


Rolig Loon wrote:

integer DetermineBoolean(){    float Seed = 100.0;    integer Random = (integer)llFrand(Seed);    return Random%2;}

 

I think I get the general logic behind this (ie. generating odd and even numbers); but what I
dont
get is why your float Seed is 100.0.

I spent a bit of time playing with your snippet, running counted iterations, and I got just as reliable results with Seed set at 2.0.

 

Link to comment
Share on other sites


anselm Hexicola wrote:

[ ..which is why I always am dismayed by the eventual size of my scripts ...]

Welcome to my world, Anselm! I wrote a lot of code over my career in engineering. Along the way, I noticed a pattern to the size of my programs during development. They would start out small, simple, and oh so broken. As I fixed 'em they'd balloon. Then I'd discover that my original concepts were, well, for lack of a better term... stupid. By the time I was done, the code was still larger than I expected at the start, but it was smaller than it was when I first got it working.

A secret to success is to be dismayed by your own own work while it amazes others.

You and I are halfway there.

;-).

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3342 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...