6 Replies - 314 Views - Last Post: 09 March 2013 - 05:07 AM Rate Topic: -----

#1 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 20
  • Joined: 20-February 13

[HELP] (int)(Math.random()*9)

Posted 08 March 2013 - 06:25 PM

number=(int)(Math.random()*9)


I know that statement generates random numbers from 0-9. But what if I want to generate starring from 2-9, instead of starting from 0?
Is This A Good Question/Topic? 0
  • +

Replies To: [HELP] (int)(Math.random()*9)

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,688
  • Joined: 27-December 08

Re: [HELP] (int)(Math.random()*9)

Posted 08 March 2013 - 06:51 PM

Generate integers from 0-7 and then add 2 to the result.
Was This Post Helpful? 2
  • +
  • -

#3 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: [HELP] (int)(Math.random()*9)

Posted 08 March 2013 - 07:01 PM

Or generate (int) Math.random( )*10

And take those in range 2-9 with an if statement

Do generation recursively

This post has been edited by burakaltr: 08 March 2013 - 07:06 PM

Was This Post Helpful? -2
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,688
  • Joined: 27-December 08

Re: [HELP] (int)(Math.random()*9)

Posted 08 March 2013 - 07:09 PM

This is a highly inefficient approach. Why bother with the extra calculations at all and use recursion, which is expensive to begin with, when it isn't necessary?
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: [HELP] (int)(Math.random()*9)

Posted 08 March 2013 - 07:53 PM

I hate using Math.random(). Math.random() uses the Random() class internally anyway, so use that. It's nicer to look at.

Apache has a MathUtils library with a class that will let you generate random numbers within a specified range, much like you can do with c#.

I don't know why I replied. I added nothing to this topic...
Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [HELP] (int)(Math.random()*9)

Posted 08 March 2013 - 08:47 PM

Random numbers are calculatedd base on integer calculation
Math.random() creates a static Random object anyhow

So if you need a random integer just create a Random object and call its nextInt() method

2-9

n = ran.nextInt(8) + 2 so may be a rule

betwen x-y

n = ran.nextInt(y-x+1) + x
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: [HELP] (int)(Math.random()*9)

Posted 09 March 2013 - 05:07 AM

http://technojeeves....-value-in-range
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1