Results 1 to 11 of 11
  1. #1
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default Trying to Generate Random number

    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");
        }
       
    }
    I'm trying to display iFrag1 with random number from 1-6.
    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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    He just copied a line of code from that page I've send in earlier thread, without reading that.

  5. #5
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    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");
        }
       
    }
    My apologies, I've rewritten it like this. Is this ok?

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb Memory Lane

    Quote Originally Posted by PeterFeng View Post
    My apologies, I've rewritten it like this. Is this ok?
    If it's from Oklahoma, it's okay.
    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.
       }
       
    }
    If you don't use SecureRandom, you may be a poster boy on Memory Lane.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    If it's from Oklahoma, it's okay.
    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.
       }
       
    }
    If you don't use SecureRandom, you may be a poster boy on Memory Lane.

    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.

  8. #8
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    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.

  9. #9
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    i see. thanks man!

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking Math.random

    What is the diff between secureRandom and 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....
    Java Code:
    import java.util.Random;
    //...
    Random radon = new Random();
    radon.nextInt();
    mtyoung you have not declare and initialize your "generator"
    already answer your question,.....

    newbies, oh the fun!
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java Code:
    Math.random()
    does much the same thing but gives a double, not nextInt()...
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 10:13 AM
  3. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 02:42 PM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •