Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Randomize Arrays to Variables?
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
stajanleafs




PostPosted: Wed Jun 04, 2008 5:06 pm   Post subject: Randomize Arrays to Variables?

hey guys... question.
i have an array and i want to import the numbers in the array into my sets of variables but i want it to be random.. i'm at a brain block here in my program. so for example c1 may equal 10 one time and another time possibly 5. but no number to repeat variables.
might this be possible through a for loop? i've tried several times with such a loop and have ome at a balnk still

Quote:
int c1;
int c2;
int c3;
int c4;
int c5;
int c6;
int c7;
int c8;
int c9;
int c10;
int c11;
int c12;
int c13;
int c14;
int c15;
int c16;
int c17;
int c18;
int c19;
int c20;
int c21;
int c22;
int c23;
int c24;
int c25;
int c26;

double [] val = {0.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000};
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Wed Jun 04, 2008 5:58 pm   Post subject: RE:Randomize Arrays to Variables?

I can't see a reason as to why you want to use hundreds of individual variables. One solution for this would be to randomly swap the values of the array (perhaps into another one, or just use the one you already have). I would not recommend using your c<n> practice, mainly because it's not the correct way to do something like this, it requires a lot of hard coding (you have to specify c<n> whatever in code each time), and it does not teach proper coding practices.

One solution would be, as I mentioned, to swap array elements at random.

Java:

double[] val = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int rnd_set;
double swapval;
for (int i = 0; i<val.length; i++)
{
        rnd_set = (int)(Math.random()*(val.length-1));
        swapval = val[i];
        val[i] = val[rnd_set];
        val[rnd_set] = swapval;
}


Now whether or not that code will work is beyond me, as I am responding to this on my laptop while Mass Effect is installing >.>

However, in theory, it should work. Basically, what it does is, while iterating through the array, it choses a random index each time within the bounds of the array, swaps the values of the chosen index and the current iteration index, and repeats. You can do that as much as you want for "extreme" randomization. Other than that, an alternative to accessing it using "int c25" is val[25] or just renaming the array to c if you really must do so.
Reality Check




PostPosted: Wed Jun 04, 2008 6:02 pm   Post subject: Re: Randomize Arrays to Variables?

What do you need this for exactly? You are essentially hard coding this and it is not good coding practice by any means. Tell me what you need it for and I'll tell you a better way to do it. However, at first glance I can see that simply swapping things from your array and using the array itself to reference values is better than referencing 26 different variables.
jeffgreco13




PostPosted: Thu Jun 05, 2008 9:30 am   Post subject: Re: Randomize Arrays to Variables?

That's right. Having that many different variables like you have is very sloppy programing and can be easily worked around.

If you are transfering the values from one array to the variables in random order then use Euphoracles example and you can even throw it in a method for easier access later.

Java:

public static double[] randomize() {
    double[] val = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    double[] c = new double[val.length];
    int rnd_set;
    for (int i = 0; i<val.length; i++)
    {
        rnd_set = (int)(Math.random()*(val.length-1));
        c[i] = val[rnd_set];
    }
    return c;
}


This code isn't quite complete but you should get the point that now the values from val are randomly picked and assigned to the array c is sequential order.
stajanleafs




PostPosted: Thu Jun 05, 2008 11:44 am   Post subject: Re: Randomize Arrays to Variables?

ok forget all the crazy ints... i need this to be doubles anyways and instead two arrays is the better idea i see. so i'll use that
is it possible i can just use a Shuffle code to change the data in the array to make it into a random new array? i can't find any examples on how to use a shuffel. any would be apreciated
jeffgreco13




PostPosted: Thu Jun 05, 2008 11:59 am   Post subject: Re: Randomize Arrays to Variables?

stajanleafs wrote:

ok forget all the crazy ints... i need this to be doubles anyways and instead two arrays is the better idea i see. so i'll use that
is it possible i can just use a Shuffle code to change the data in the array to make it into a random new array? i can't find any examples on how to use a shuffel. any would be apreciated


Are the values in the array going to be static, and that you just want to re-arrange them randomly a couple times? or are those values going to be inputted from the user?

I don't see why you don't implement the code that Euphorical and I had suggested because it does exactly what you have asked.
It organizes the given data from Array1, 'shuffles' it and then assigns it into Array2. Doesn't get much simpler.

Oh and if any mods are out there, if they wouldn't mind throwing in a return statement in my Java code above:
code:
return c;


Thanks a bunch.
Reality Check




PostPosted: Thu Jun 05, 2008 12:34 pm   Post subject: Re: Randomize Arrays to Variables?

Just write a method that swaps values in your array and returns the newly swapped array. The one jeff wrote for you should work fine.
stajanleafs




PostPosted: Thu Jun 05, 2008 2:51 pm   Post subject: Re: Randomize Arrays to Variables?

that's fine and all, but it repeats. i used it and some of the varibles repeat. is there a way for no repeats?
the variables are static yes but i just need them to be randomized

and i keep getting errors on the public static randomize.. do i need to import java.util.*?
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Thu Jun 05, 2008 9:51 pm   Post subject: RE:Randomize Arrays to Variables?

The method I posted should not create duplicates, as far as I can tell.
jeffgreco13




PostPosted: Fri Jun 06, 2008 8:10 am   Post subject: Re: Randomize Arrays to Variables?

what kind of errors are u getting on the public static randomize? I've declared a return statement in my function which means that in the public static header you have to specify the return type which in ur case is double[].

Maybe, depending on the rest of ur program, it needs to be declared as an instance. Look it up, and compare it to the error that you are getting when you compile.
stajanleafs




PostPosted: Fri Jun 06, 2008 11:40 am   Post subject: Re: Randomize Arrays to Variables?

Quote:
import java.io.*;
import java.text.*;
import hsa.Console;
class Test
{

static Console c;
public static void main (String [] args) throws IOException
{

randomize();




}


public static double [] randomize ()
{
c = new Console ();
double [] cs1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double [] cs = new double [cs1.length];
int rnd_set;
for (int i = 0 ; i < cs1.length ; i++)
{
rnd_set = (int) (Math.random () * (cs1.length - 1));
cs [i] = cs1 [rnd_set];

}
c.println (cs [0] + " " + cs [1] + " " + cs [2] + " " + cs [3] + " " + cs [4] + " " + cs [5] + " " + cs [6] + " " + cs [7] + " " + cs [8] + " " + cs [9]);
double c1 = cs [0];
//c.println (c1);

return cs;
}
}


and the output is
Quote:

2 7 6 4 3 1 5 9 7 4


see i get repeats[/list]
Euphoracle




PostPosted: Fri Jun 06, 2008 3:05 pm   Post subject: RE:Randomize Arrays to Variables?

The method you are using can duplicate because you don't blacklist the indices that you've already used. Try using the method I suggested and see if the results repeat. You can easily copy the array if you feel it necessary with:

Java:

double[] newarray = new double[oldarray.length];
for (int i=0;i<oldarray.length;i++) newarray[i]=oldarray[i];
stajanleafs




PostPosted: Fri Jun 06, 2008 3:14 pm   Post subject: Re: Randomize Arrays to Variables?

I'm asuming you're method works because it runs without isses but using this i can't sem to get it to work.. how can i output the randomized array elements? that's pretty much it.. thanks for the help though much apeciated!

Quote:
double[] val = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int rnd_set;
double swapval;
for (int i = 0; i<val.length; i++)
{
rnd_set = (int)(Math.random()*(val.length-1));
swapval = val[i];
val[i] = val[rnd_set];
val[rnd_set] = swapval;

}
c.println (val [0] + " " + val [1] + " " + val[2] + " " + val [3] + " " + val [4] + " " + val[5] + " " + val [6] + " " + val[7] + " " + val [8] + " " + val [9]);
shadowman571




PostPosted: Fri Jun 06, 2008 4:02 pm   Post subject: Re: Randomize Arrays to Variables?

is there a reason for the many different values? because if not i would recomend using a vector to store them
jeffgreco13




PostPosted: Mon Jun 09, 2008 8:18 am   Post subject: Re: Randomize Arrays to Variables?

What do you mean you cant get it to work?

You're going to have to give us details on what's happening because I get mine to work when I try it.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: