Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

[Java] Randomly choosing array elements

Name: Anonymous 2011-11-24 14:22

So I have a program that generates random passwords using the ASCII values from 33 - 126, but there are certain ones I don't want to use (34, 35, 100, whatever)

What I initially did was use Math.random to generate a number between 33 - 126, then an if statement to make sure the generated password didn't contain any of the forbidden characters.

This is extremely inefficient and shitty, and I would much rather make it so I have an array of all the valid ASCII values, and it randomly chooses, say, 6 of them and throws them in to another char array to hold the password.

I am quite new at this, so I don't really know how to do that.

This is the for loop that I am currently using to pick values between 33 and 126:

for(int i = 0; i < tempPass.length; i++)
{
   tempPass[i] = (char)((int)(94 * Math.random()) + 33);
}

How do I change this to pick values from 0 to 76 (the elements of the valid char array)??

Name: Anonymous 2011-11-24 14:26

How the fuck is this not the easiest thing in existence?

You know how to generate a random number in a given range. You know how to index arrays. Look, both of these things are clearly visible in your code-tag-less example. Leave /prog/ now.

Name: Anonymous 2011-11-24 14:29

something like

password = charArray[mathrandomshit]

Name: Anonymous 2011-11-24 14:38

>>2
I think he copied the code from someone.
>>1
s/94/77/
s/ \+ 33//

Name: Anonymous 2011-11-24 14:38

"abcdefghijklmnopqrstuvwxyzABChaxmyanus".charAt(...);

Name: Anonymous 2011-11-24 14:43

Here, OP.
#include <stdio.h>
#include <time.h>

int main(void)
{
    char values[1024];
    char evil[] = {34, 35, 100, /* whatever, */ 0};
    int i, j;
    int index = 0;
    int length = 30;    /* length of your password */

    /* seed random */
    srand(time(NULL));

    /* list possible characters from
     * 33 to 126, excluding the evil
     * ones
     */
    for(i = 33; i <= 126; i++)
    {
        values[index++] = i;
        for(j = 0; evil[j]; j++)
            if(evil[j]==i)
                --index;
    }

    /* write out the random password */
    for(i = 0; i < length; i++)
        putchar(values[rand() % index]);
   
    return 0;
}
.

BTW that's in C, not in Java.

Name: Anonymous 2011-11-24 14:50

>>6

samples for OP's viewing pleasure:

-45vbu{~@6h!{$;t+?J8V)0\%X?Q9P
7Cb%q}wG|86@oiA+8I!_:1gGkC}aLo
3<3M>\L6[$&oAMX;Un<0OS>7R*WQC%
xT>,YY@'Gz)&j~PD:]j/l:_+)4j8=.
;6:1vrCoPBj1E/hM+!cs/+eG3l3T%(
;t;R'i|~X)Az7?+|j3W)'>1jby1MN3
RA_EL>gI=@O6s?R{<u/K{khe0&^>Qe
)[(@!Q[gxP^$eRwo)4e6]<X}Xf$8[-
z<g!ZA,n_\@?]\JULPAj>V]t/75h'H
r~b4YuRbc3@[+U9-b>Z\)Q4>}xR4`V

Name: Anonymous 2011-11-24 18:12

PasswordFactoryFactory.getInstance(PasswordFactoryFactory.defaultInstanceDescriptionString).getFactory(Locale.getLocale("en-US")).createBuilder().setPassword(random).getResult()

Don't change these.
Name: Email:
Entire Thread Thread List