Computer Science Canada

Randomize Arrays to Variables?

Author:  stajanleafs [ 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};

Author:  Euphoracle [ 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.

Author:  Reality Check [ 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.

Author:  jeffgreco13 [ 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.

Author:  stajanleafs [ 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

Author:  jeffgreco13 [ 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.

Author:  Reality Check [ 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.

Author:  stajanleafs [ 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.*?

Author:  Euphoracle [ 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.

Author:  jeffgreco13 [ 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.

Author:  stajanleafs [ 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]

Author:  Euphoracle [ 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];

Author:  stajanleafs [ 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]);

Author:  shadowman571 [ 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

Author:  jeffgreco13 [ 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.

Author:  stajanleafs [ Mon Jun 09, 2008 11:12 am ]
Post subject:  RE:Randomize Arrays to Variables?

i get...
attempt to use a null pointer (java.lang.NullPointerException)

Author:  Euphoracle [ Mon Jun 09, 2008 5:29 pm ]
Post subject:  RE:Randomize Arrays to Variables?

Line?

Edit: Try changing the -1 at the Math.Random bit to -2. I overlooked that length is last index+1 essentially.

Author:  stajanleafs [ Mon Jun 09, 2008 8:06 pm ]
Post subject:  RE:Randomize Arrays to Variables?

ok well thanks for the answers guys.. i came up with a solution o my own losely based on what you guys gave me and some Java examples of things....
thanks for helping


: