Results 1 to 11 of 11
Thread: Trying to Generate Random number
- 01-13-2009, 07:49 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
Trying to Generate Random number
I'm trying to display iFrag1 with random number from 1-6.Java Code:import java.util.Random; public class mainMemory { public static void main(String[] args) { //Int Fragmentation int iFrag2=2, iFrag3=3, iFragT ; [COLOR="Red"][B] int iFrag1 = generator.nextInt(6) + 1;[/B][/COLOR] iFragT=iFrag1+iFrag2+iFrag3; //Ext Fragmentation int eFrag1=100, eFrag2=300, eFrag3=600, eFragT ; eFragT=eFrag1+eFrag2+eFrag3; // System.out.println("Internal Frag: " +iFrag1 +" Kb"); System.out.println("Internal Frag: " +eFragT +" Kb"); } }
In JCreator it returns error at the line in red:
1) cannot find symbol variable generator
2) operator + cannot be applied to generator.nextInt, int
3) imcompatible types
- 01-13-2009, 08:16 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Just try to sort out random number generating. Later added into your application.
- 01-13-2009, 08:50 AM #3
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
you have not declare and initialize your "generator"
- 01-13-2009, 08:54 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
He just copied a line of code from that page I've send in earlier thread, without reading that.
- 01-13-2009, 05:05 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
My apologies, I've rewritten it like this. Is this ok?Java Code:import java.util.Random; public class mainMemory { public static void main(String[] args) { Random generator = new Random(); //MemorySize int mem1 = generator.nextInt(999); int mem2 = generator.nextInt(999); int mem3 = generator.nextInt(999); int mem4 = generator.nextInt(999); int mem5 = generator.nextInt(999); //Memory Allocation int memA1 = generator.nextInt(999); int memA2 = generator.nextInt(999); int memA3 = generator.nextInt(999); int memA4 = generator.nextInt(999); int memA5 = generator.nextInt(999); //Int Fragmentation int iFrag= (mem1-memA1)+(mem2-memA2)+(mem3-memA3)+(mem4-memA4)+(mem5-memA5); //Ext Fragmentation int eFrag1=100, eFrag2=300, eFrag3=600, eFragT ; eFragT=eFrag1+eFrag2+eFrag3; // System.out.println("| "+mem1 +"Kb | " +mem2 +"Kb | " +mem3 +"Kb | " +mem4 +"Kb | " +mem5 +"kb"); System.out.println("| "+memA1 +"Kb | " +memA2 +"Kb | " +memA3 +"Kb | " +memA4 +"Kb | " +mem5 +"kb \n\n"); System.out.println("Internal Frag: " +iFrag +" Kb"); System.out.println("Internal Frag: " +eFragT +" Kb"); } }
- 01-13-2009, 05:27 PM #6
Memory Lane
If it's from Oklahoma, it's okay.
If you don't use SecureRandom, you may be a poster boy on Memory Lane.Java Code:import java.security.SecureRandom; public class MemoryLane { public static void main(String[] args) { SecureRandom rabi_dambi = SecureRandom.getInstance("SHA1PRNG"); //MemorySize int mem1 = rabi_dambi.nextInt(999); int mem2 = rabi_dambi.nextInt(999); int mem3 = rabi_dambi.nextInt(999); int mem4 = rabi_dambi.nextInt(999); int mem5 = rabi_dambi.nextInt(999); //Memory Allocation int memA1 = rabi_dambi.nextInt(999); int memA2 = rabi_dambi.nextInt(999); int memA3 = rabi_dambi.nextInt(999); int memA4 = rabi_dambi.nextInt(999); int memA5 = rabi_dambi.nextInt(999); //... remainder as you had it. } }Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 01-14-2009, 05:10 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
I found this on suns website, but can someone reduce it to layman terms? What is the diff between secureRandom and random?
This class provides a cryptographically strong pseudo-random number generator (PRNG). A cryptographically strong pseudo-random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Modules, section 4.9.1. Additionally, SecureRandom must produce non-deterministic output and therefore it is required that the seed material be unpredictable and that output of SecureRandom be cryptographically strong sequences as described in RFC 1750: Randomness Recommendations for Security.
Like other algorithm-based classes in Java Security, SecureRandom provides implementation-independent algorithms, whereby a caller (application code) requests a particular PRNG algorithm and is handed back a SecureRandom object for that algorithm. It is also possible, if desired, to request a particular algorithm from a particular provider. See the getInstance methods.
- 01-14-2009, 05:53 AM #8
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Generally, if you need to use SecureRandom, you'll know you need to. Otherwise, don't worry about it.
Although the numbers it produces seem "random to the naked eye", Random actually uses a fairly simple formula (a so-called Linear Congruential Generator). So it would be no good using Random to generate, say, session numbers for a web site. If you did, then Person A could log on to the site, see the session number they were given, and then guess Person B's (or lots of other people's) session number. SecureRandom uses an algorithm that is slower, but which makes it much more difficult for an observer to guess the next or previous numbers in the sequence.Neil Coffey
Javamex - Java tutorials and performance info
- 01-14-2009, 06:10 AM #9
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
i see. thanks man!
- 01-14-2009, 06:13 AM #10
Math.random
I was just havin fun with newbies, I hate these fancy tools and take a few swipes here and there at them. Suffice it to say, if you use the fancy tools - you still have to learn how to code. If you are not protecting your Pit Bull from the neighbor's cat, you may use Random class freely. Your Pit Bull won't mind....What is the diff between secureRandom and random?Java Code:import java.util.Random; //... Random radon = new Random(); radon.nextInt();
already answer your question,.....mtyoung you have not declare and initialize your "generator"
newbies, oh the fun!Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 01-14-2009, 08:37 AM #11
does much the same thing but gives a double, not nextInt()...Java Code:Math.random()
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
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 -
Random number help
By jgonzalez14 in forum New To JavaReplies: 5Last Post: 09-16-2008, 09:13 AM -
Random number
By jithan in forum Advanced JavaReplies: 1Last Post: 06-13-2008, 01:42 PM -
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:23 AM -
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks