Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Getting a random sign

Posted on 2004-08-03
Medium Priority
496 Views
Hi all,

I want to get a randomly generated binary choice:  either -1 or 1.  This function does it, but is there is a 'nicer' way?

private int getRandomSign() {
if (Math.random() <= 0.5) return -1;
else return 1;
}
}
0
Question by:Dawkins
• 3
• 2
• 2
• +2

LVL 14

Assisted Solution

sudhakar_koundinya earned 100 total points
ID: 11706737
That is the nicer way :)
0

LVL 35

Expert Comment

ID: 11707086
Can you define "nicer"? Your code looks fine.
0

LVL 35

Accepted Solution

girionis earned 100 total points
ID: 11707122
Maybe you could do

private int getRandomSign() {
if (Math.random() < 0.5) return -1;
else return 1;
}
}

since it can return a value equal to zero but never one.
0

LVL 14

Expert Comment

ID: 11707291
I haven't understand the difference between if (Math.random() < 0.5) return -1; and if (Math.random() <= 0.5) return -1;

both results gives the value less than one only.

Regards
Sudhakar
0

Author Comment

ID: 11707310
>>>  Maybe you could do...

Good point, it should be < rather than <=. I'll change that

>>> Can you define "nicer"? Your code looks fine.

well basically I am teaching myself how to program in Java and know no Java programers in real life so have no one to ask "is this completely ridiculous", so I come here instead :)  By nicer I mean "more elegant, simpler, less code, there's already a java library function that does that" etc

Seems like I've already found the best way so I'll give it another 30 minutes to make sure and then give you two the points.
0

LVL 6

Expert Comment

ID: 11707336

Here's another approach that may be closer to what you want:

private int getRandomSigh() {
return (int) (Math.signum(0.5-Math.random())) ;
}

An additional note:  using "<= 0.5" favors -1  VERY slightly  since Math.random() returns values between 0 and .999...
0

LVL 6

Expert Comment

ID: 11707408
There's a problem with my posted solution: there's a very slight chance it will return 0. If you can't live with that, here's an amendment:

private int getRandomSign() {
int sign = (int) (Math.signum(0.5-Math.random())) ;
return sign = 0 ? 1 : sign ;
}
0

LVL 15

Expert Comment

ID: 11707712
nice & short:

private int getRandomSign() {
return (Math.random()<0.5) ? 1 : -1;                           // ?: operator same as in C
}

regards JakobA
0

Author Comment

ID: 11708880
Thanks everyone, I'll keep it as I originally had it but put < 0.5 instead to ensure the split is exactly 50/50
0

LVL 35

Expert Comment

ID: 11712677
:)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeâ€¦
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define â€¦
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
###### Suggested Courses
Course of the Month11 days, 19 hours left to enroll