random number generator

common random number generator

Page 1 of 1

5 Replies - 3291 Views - Last Post: 22 December 2010 - 03:39 PM Rate Topic: -----

#1 djronbxs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-November 09

random number generator

Posted 22 December 2010 - 04:32 AM

Hi,

I have implemented an array a binary search tree and an AVL tree. Ive made a random number generator to insert 20 randomly generated numbers into the array, now what I would like to do and im finding it difficult is that the same numbers generated and insrted into the array be inserted into the binary search tree and the AVL tree. in other word im trying to make a random number generator common to the array, binary search tree and the AVL tree.

can you help me please ?

thanks
ron

Is This A Good Question/Topic? 0
  • +

Replies To: random number generator

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2066
  • View blog
  • Posts: 4,297
  • Joined: 11-December 07

Re: random number generator

Posted 22 December 2010 - 05:00 AM

Really, you implemented an AVL tree and you can't do this?

for(Number n : array) {
  binarySearchTree.add(n);
  avlTree.add(n);
}


If the problem is trying to add an int or double when you need an object you can create wrapper Integer and Double objects to add to your data structures.

This post has been edited by cfoley: 22 December 2010 - 04:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 djronbxs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-November 09

Re: random number generator

Posted 22 December 2010 - 05:49 AM

         
        Random generator = new Random();                                            // Create a random generator.

        int l = generator.nextInt(20);
        
        AvlNode root = AvlTree.CreateAvlNode(l);                                    // Creating the Avl tree root
        
        for (int i = 0; i < arraycapacity; i++)                                     // Loop through all the array

                {

                    Array[i] = l;                                                  // Insert a random number between 0-20.
                    theTree.insert(l, 0);
                   

                }

        for (int i = 1; i < arraycapacity; i++)

            {
                AvlTree.AvlInsert(root, AvlTree.CreateAvlNode(l));
            } 



to insert into the Array the BST and the AVL I made the above code but the numbers in the AVL and the BST are not the same as the numbers in the array. I made another separate for loop to insert into the avl because im inserting the root separetly so I need to decrease one from the 20 numbers.

what im doing wrong ?

thanks
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: random number generator

Posted 22 December 2010 - 03:07 PM

You are inserting arrayCapacity times the same number

        Array[i] = l;                           // Insert a random number between 0-20.  
        theTree.insert(l, 0);  


you need
        l = generator.nextInt(20);
        Array[i] = l;                           // Insert a random number between 0-20.  
        theTree.insert(l, 0);  


Was This Post Helpful? 0
  • +
  • -

#5 fariba_yoo  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 89
  • Joined: 17-May 08

Re: random number generator

Posted 22 December 2010 - 03:37 PM

View Postpbl, on 22 December 2010 - 09:07 PM, said:

You are inserting arrayCapacity times the same number

        Array[i] = l;                           // Insert a random number between 0-20.  
        theTree.insert(l, 0);  


you need
        l = generator.nextInt(20);
        Array[i] = l;                           // Insert a random number between 0-20.  
        theTree.insert(l, 0);  




But he has write
 l = generator.nextInt(20); 

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: random number generator

Posted 22 December 2010 - 03:39 PM

View Postfariba_yoo, on 22 December 2010 - 04:37 PM, said:

But he has write
 l = generator.nextInt(20); 

only once oustide the loop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1