Results 1 to 12 of 12
Thread: Random
- 09-29-2008, 09:37 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
- 09-29-2008, 09:51 AM #2
Member
- Join Date
- Sep 2008
- Posts
- 36
- Rep Power
- 0
Hey, to generate random numbers you can declare a variable,
for instance
public class Random
{
private int Number1;
public Random()
{
Number1 = (int)(Math.random() * 10);
//Generates a value from (0.1 to 1.0) * 10 thus numbers 1-10
}
public int getNumber1()
{
return Number1;
}
But that's assuming it's not the main if it's the main then:
public class Main
{
public static void main(String[]args)
{
//Now make objects of your classes
//For instanse
Class1 cs = new Class1();
//Generate a number
int x = (int)(Math.random() * 10);
//Now put it in the method you want in the class by
cs.setmethodName(x);
System.out.println("The number is "+ cs.getmethodName());
}
}
- 09-29-2008, 10:34 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually random() method generates values less than 1.0. It never generate 1.
- 09-29-2008, 10:50 AM #4
Member
- Join Date
- Sep 2008
- Posts
- 36
- Rep Power
- 0
Really?
Wow, thanks for the info Eranga!;)
So, what's the maximum 0.9?
- 09-29-2008, 10:53 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, maximum is 0.999999999999999
- 09-29-2008, 10:55 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
Hi
Thanks for your reply.
shall i use the setter and getter methods to store that number andshall i get the same random number in 5 different methods in didfferent classes by calling tha getmethod. Is it posiible to any on-line sites(like any registreation or booking sites)
- 09-29-2008, 11:15 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
i don't think it's a good idea.
- 09-29-2008, 12:22 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
please give proper way to do that.
- 09-29-2008, 12:26 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you exactly want to do? Generate a random number between 1 and 10?
- 09-29-2008, 12:58 PM #10
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
I have to generate the number randomly in one method and that value should reflect in 5 more methods, in same or another class.
For that i have to store in one setmethod and get method.
Now should i call this getmethod in another method to maintain the same random value in all 5 methods.
- 09-29-2008, 01:57 PM #11
Put randomizer in one class
Start by writing one class that does one thing, generate random numbers. Then have the other class constructors take a variable of type class you wrote - just make sure all classes get the instance from the same call to new if all classes need the random numbers to match up, get and set may need to be called generate / get as one would not likely set the random number from other classes. Sometimes this may be called producer-consumer pattern. How many random numbers are needed and to they need to match up in all places?
Code from Loop should get you going, with note from eranga that random only generates values < 1.0000000 eg 0.999897758 or something. Simple but takes a moment to grasp and get it right.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 09-30-2008, 03:39 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you want to use the same randomly generated number in five different locations, you have to use get and set method. It's nothing to say that if you call the random number generating method five times, you get five different values.
But first of all, as Nichol says in his last post, deal with a one class first and see how it's easy/difficult to implement in your application.
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Using Random
By razmyasdfg in forum CLDC and MIDPReplies: 1Last Post: 07-27-2008, 10:47 PM -
Random problem
By mcal in forum New To JavaReplies: 18Last Post: 01-24-2008, 04:00 AM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random generation
By carlos123 in forum New To JavaReplies: 10Last Post: 01-09-2008, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks