Lisa Austin wrote:Okay I think I'm blind .. The Quora solution seems to be working for me.
Liutauras Vilda wrote:Do you understand why you got negative numbers occasionally? That should have helped you in looking for solution.
No.Lisa Austin wrote:. . . Because random can include negatives?
Sorry, I made a mistake there.About two hours ago, I wrote:2³¹ − 1 = 2147483648 . . .
salvin francis wrote:
Lisa Austin wrote:Okay I think I'm blind .. The Quora solution seems to be working for me.
Hi Lisa, let's leave the random part for a moment. Have a look at the program below:
output:
Do you understand why we get a negative output ?
Campbell Ritchie wrote:What are the ranges of number you want? You realise an int only goes up to 2³¹ − 1 = 2147483648, as Salvin has already told you. I think they are right on SO that the second version on Quora is incorrect. I think the first version on Quora is correct, but it gives you numbers in the range 1,000,000...9,999,999, all of which fall nicely within the range of an int. So they are answering a different question from yours.
The only method in the Random object with a bound is nextInt(). You cannot get it to fill the range 1,000,000,000...9,999,999,999 but you can call it twice, get two five‑digit numbers and put them together. Only not as an int.
Lisa Austin wrote:
Campbell Ritchie wrote:What are the ranges of number you want? You realise an int only goes up to 2³¹ − 1 = 2147483648, as Salvin has already told you. I think they are right on SO that the second version on Quora is incorrect. I think the first version on Quora is correct, but it gives you numbers in the range 1,000,000...9,999,999, all of which fall nicely within the range of an int. So they are answering a different question from yours.
The only method in the Random object with a bound is nextInt(). You cannot get it to fill the range 1,000,000,000...9,999,999,999 but you can call it twice, get two five‑digit numbers and put them together. Only not as an int.
I hope I'm making sense. I often have trouble trying to communicate what I'm thinking.
You asked what range and pointed out that it gives numbers in the range of 1,000,000 .... 9,999,999 but you say the first answer is answering a different question than mine. I think this maybe where I fail to communicate and understand properly?
I think the range of numbers is what I'm looking for as long as I can get 10 digits total out of it. But I think I must be missing your point.
That will fit into the positive range of the int datatype.Lisa Austin wrote:. . . numbers in the range of 1,000,000 .... 9,999,999
Ten digits means to me you want numbers in the range 1,000,000,000...9,999,999,999. That won't fit into the int datatype. None of the links you quoted deals with numbers that large.but you say the first answer is answering a different question than mine. . . . the range of numbers is what I'm looking for as long as I can get 10 digits total out of it. . . .
Campbell Ritchie wrote:No, 2³¹ + 1 isn't 2147483648. Nor is 2³¹ − 1 as I wrote earlier by mistake. Nor is 2³¹ because there ain't no such int number as 2147483648. We are not in the realms of ordinary arithmetic where you are dealing with the set of all natural numbers or of all integers. We are dealing with a 32‑bit two's complement number. Haven't you been taught about the limits of two's complement numbers? Haven't you been taught what happens when you try to go beyond the extremes of the range?
There are hundreds of sources where you can remind yourself of those ranges, without my making mistakes: this is the example in the Java® aLanguage Specification.
Bear Bibeault wrote:It's all about fixed-register math: two's complement
ints have to fit into 32-bits, so the lower half of the range (where the highest order bit is set to 0) represents positive values, while the upper half (where the highest order bit is set to 1) represents the negative values.
That won't give you a ten‑digit number. It will only give you a seven‑digit number. I don't like doing arithmetic with Math#random to get an int because:-Lisa Austin wrote:. . . This is what I felt worked for me .
Richard?Lisa Austin wrote:. . . As pointed out by Richard . . .
That's a pleasureI appreciate it.
Campbell Ritchie wrote:
That won't give you a ten‑digit number. It will only give you a seven‑digit number. I don't like doing arithmetic with Math#random to get an int because:-Lisa Austin wrote:. . . This is what I felt worked for me .
1: Such arithmetic is potentially error-prone. 2: You are using two “random” ints to create the double and then converting it back to an int. Why do all that additional work when you can get an int directly? Your generateMyNumber() method is badly named because it doesn't generate and return a number; it prints it to screen
Campbell Ritchie wrote:
Richard?Lisa Austin wrote:. . . As pointed out by Richard . . .
That's a pleasureI appreciate it.
Campbell Ritchie wrote:
That won't give you a ten‑digit number. It will only give you a seven‑digit number. I don't like doing arithmetic with Math#random to get an int because:-Lisa Austin wrote:. . . This is what I felt worked for me .
1: Such arithmetic is potentially error-prone. 2: You are using two “random” ints to create the double and then converting it back to an int. Why do all that additional work when you can get an int directly? Your generateMyNumber() method is badly named because it doesn't generate and return a number; it prints it to screen
More details about not doing arithmetic with Math#random in this old post and the other links in it, and . . .Lisa Austin wrote:. . . arithmetic with Math#random . . . Thank You
That will give you numbers between 1,000,000,000 and 1,999,999,999 inclusive, a lot less than simply using ten digits. If that is the range you want, I would prefer to use a Random object:-You can use new Random() or one of its subclasses, which you can find from its documentation.Lisa Austin wrote:. . .
Campbell Ritchie wrote:The origin of the name Ritchie is probably from Richard, so you aren't that far out after all
More to the point: have you worked out why you can't get ten digits out of the methods you have tried so far?
Lisa Austin wrote:
Campbell Ritchie wrote:The origin of the name Ritchie is probably from Richard, so you aren't that far out after all
More to the point: have you worked out why you can't get ten digits out of the methods you have tried so far?
I'm still working at it. I get it's binary. The methods I was trying where I was using 1234567890 which falls into my int range and is 31 bits. Random uses bits which I take that the values I'm using could randomly add up to a bit starting with 1 or 0 making it negative or positive. I do remember working with binary and two's complementary back in the day but as I'm sure you can tell that math isn't my strong suite so it takes me breaking things down in my head to understand. I'm still working through this which I don't mind because I want to understand.
Consider Paul's rocket mass heater. |