Solved

Getting a random sign

Posted on 2004-08-03
10
480 Views
Last Modified: 2006-11-17
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
Comment
Question by:Dawkins
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 25 total points
ID: 11706737
That is the nicer way :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11707086
Can you define "nicer"? Your code looks fine.
0
 
LVL 35

Accepted Solution

by:
girionis earned 25 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

by:sudhakar_koundinya
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

by:Dawkins
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:rpnman
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

by:rpnman
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

by:JakobA
ID: 11707712
nice & short:

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

regards JakobA
0
 

Author Comment

by:Dawkins
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

by:girionis
ID: 11712677
:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 76
count11 challenge 6 47
wordcount challenge 11 76
Java DateChooser? 2 11
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now