samedi 1 octobre 2016

assigning random numbers to arraylist

I'm trying to make a thing to assign random Stats to Characters in DnD I have the list of strings like this,

 List<String> list = new ArrayList<String>();
        list.add("STR");
        list.add("DEX");
        list.add("CON");
        list.add("INT");
        list.add("WIS");
        list.add("CHA");




Python error 'string index out of range' word scrambler

I'm trying to make a word scrambler and I have came across the error 'string index out of range'. I asked about this error earlier and it got fixed but now it has came back again and for the life of me I cant figure out what it is. My code is: import random

a = input('Enter word ->')
rand = []
before = []
after = []
count = 0
while count <= len(a):
    z = len(a)
    rand.append(random.randint(1,z))
    before.append(a[count])
    count += 1


print(rand, before, inp)

id really appreciate it if anyone could help. thanks




Android Studio - How to create a back button on randomly selected strings?

I have an array of strings that I am displaying in my activity using a random number every time a button called Next is clicked. How do I create another button called Back, that simply displays the previously or last generated strings in the order they were randomly generated?. So that instead of clicking next to generate another string, I can click Back at any time to view all the previous strings one by one and then click Next again to continue the random selection.




Is it generally accepted to implement your own random functions ? (C++)

Suppose you are working on a (serious) project that requires random numbers.

Lines like:

int num = rand() % 6 + 5; //random number between 5 - 10

are (obviously) not going to appear there. The C++11 header <random> beats this old C-Style without any doubts.

std::random_device rd;
std::mt19937 engine(rd());
std::uniform_int_distribution<int> generate(5, 10);
int num = generate(engine);

But when working in a group, I can see 2 problems with this methode:

  • The Syntax is wierd and hard to read.

  • It's too complex for such an often simple task.

So it would be really nice to have a function (based on this methode) looking like this:

int getRandom(int range_begin, int range_end);

As far as i'm concerned, there is no such STL function. So I'm asking for your experience: would it be ok to implement this function on your own? Maybe export into a seperate header, then overload it for doubles and float etc. I am asking this question, because in this modern C++ time, where it's standard to use STL functions, this is a situation where the syntax actually forces you to implement on our own! Thanks for your help & experience on this topic.




C# pick per random out of an enum, but cupple it with if

So I have to programm Black Jack as a console programm in C#. I added an enum with all the cards in it (without colors. just numbers, ace and picure card) and give them values. then i made a method to return a random card. this works fine. but in the second step of the task i have to ad the picture cards and i have to take care of the count of picture cards in the deck. the deck is 52 cards large and 12 of them are picture cards. i tried the following but it wont work.

class Program
{
    static Random rnd = new Random(); //initialisierung Zufallsgenerator
    static ECARDS DrawRandomCard() //Methode zum zufälligen ziehen einer karte aus dem enum ECARDS
    {
        if ((int)DrawRandomCard() < 12)
        {
            return ECARDS.Picture;
        }
        else
        {
            return (ECARDS)rnd.Next(2, 11);
        }            
    }



    enum ECARDS { Zwei = 2, Drei = 3, Vier = 4, Fünf = 5, Sechs = 6, Sieben = 7, Acht = 8, Neun = 9, Zehn = 10, Ass = 11, Picture = 99 }; // enum mit allen verfügbaren karten

im a newbie so maybe its very easy for you. thanks for your help. (and sorry if my english isnt the best ;)




Generate random non-repeating numbers

I'm trying to figure out how to generate a random, non-repeating number. I thought about using randint to create a number, put it in a list (repeat that for the number of place values I need), then put those lists into another list and use a while loop to check the list for repeats of the internal lists. If any occur the program just creates a new number for one of the internal lists that is repeated. Is there an easier/ less complex way of doing this?




Code that generates a random 10 digit filename and creates the file

I tried using:

import random
filenamemaker = random.randint(1,1000)

all help would be great thanks :)