dimanche 1 novembre 2015

How to generate a random, non-repeating string C++ [duplicate]

This question already has an answer here:

I need to generate a code of specified length, and it has to be random and non repetitive, so far, I have the random string but it's repeating the letters

Here is my code;

#include <string>
#include <cstdlib>
#include <ctime>

class random_text_generator
{
public:
random_text_generator(const std::string& str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
: m_str(str)
{
    std::srand(std::time(0));
}   
std::string operator ()(std::size_t len = 1)
{
    std::string seq;
    std::size_t siz = m_str.size();
    if(siz)
        while(len--)
            seq.push_back(m_str[rand() % siz]);
    return seq;
}
private:        
std::string m_str;
};

#include <iostream>

int main(void)
{
using namespace std;
random_text_generator rtg;
for(size_t cnt = 0; cnt < 7; ++cnt)
  {
    cout << rtg(cnt) << endl;
  }
}

Again, this code generates the string but it repeats some letters, for example the output would look like this

**OUTPUT**
A K X K Y

See that the K is repeating.




C generate random string of max length

I would like to generate a random string of only lower-case ASCII characters (meaning lower case letters, numbers, and other ASCII characters; just no upper-case lettesr).

The max length of the string should be 587 characters (including null terminator).

How would I go about doing this?

Thanks




Generate random numbers exaclty once

I need to create a set of random numbers between 0 and 800. The problem is at the moment that I need to do this fast and each number shall be returned only once.

My current approach is:

  • Create a std::vector containing the numbers from 0 to 800
  • Pick a number using numberVector[rand() % numberVector.length()]
  • Delete this number from the vector

I have to do this very often and my current approach is slow. Is there some way to speed things up here?




PNRG algorithm implementation in PHP and C#

I've created a procedural image generator that uses the default PNRG built in PHP5.

I can set the seed with mt_srand($id); and get always the same numbers sequence (with mt_rand(0,255);).


What I need:

a PNRG implementation that will work the exact same way in PHP and C#


Example:

PHP:

mt_srand(123);
echo mt_rand(0,255); //returns 34
echo mt_rand(0,255); //returns 102
echo mt_rand(0,255); //returns 57
echo mt_rand(0,255); //returns 212
echo mt_rand(0,255); //returns 183

C#:

setSeed(123);
print getRand(0,255); //returns 34
print getRand(0,255); //returns 102
print getRand(0,255); //returns 57
print getRand(0,255); //returns 212
print getRand(0,255); //returns 183

( ^ function names are not referring to existing ones, named just for example's sake )




java random 3 input prevent output same [duplicate]

This question already has an answer here:

i was using the code below to random pick 3 digit from 0~9, but some of the times it will generate 2 or 3 same number(like 1,1,2 or 8,8,8), can i prevent it? i just want it like 1,2,3.

Random random = new Random();

int[] sequence = new int[3];

for (int i = 0; i < 3; i++)
    sequence[i] = Math.abs(random.nextInt(9));

System.out.println("Input: ");
for (int i = 0; i < 3; i++)
    System.out.print(sequence[i] + " ");




Two dice throwing game issue

My program needs to throw 2 dices and when both are 6 it gets out of the loop and show me how many times it tries, but for some reason I get very wierd try numbers like, most of them are "it took 1-3 tries".. Help plz.

public class SheshBesh {
public static void main(String[] args){

    int r1 = 0;
    int r2 = 0;
    int tries = 0;

    while (r1 != 6 && r2 != 6) {
        r1 = (int)(Math.random() * 6) + 1;
        r2 = (int)(Math.random() * 6) + 1;
        tries = tries + 1;
    }

    System.out.println("Shesh-Besh apears... it took " + tries + " tries.");


}

}




samedi 31 octobre 2015

Text adventure game--randomly connecting rooms together - C

I'm trying to create a text adventure game that 7 rooms, with the information saved in files. This question IS similar to Connect Rooms Randomly in Adventure Game however the answer didn't exactly help me. I've gone about my program in a different way than that OP so I'm not sure how to use that answer to help me.

The idea is you have 7 rooms, named say A, B, C, D, E, F, and G. After the rooms are created, I need to randomly connect them to each other. Each room needs between 3 and 6 random connections. If room A is connected to B, C, and D, each of those rooms should be connected to A. This information is then saved to a file which is read later.

The code I have for this section so far is:

    char connections[7][7];
    int j = 0;
    int randomRoom;
    for (j = 0; j <= randConnections; j++) {
             randomRoom = rand() % 10;
             if (randomRoom == randName) {
                     randomRoom = rand() % 10;
             } else {
                    connections[j] = names[randomRoom];
    }

randConnections is a random int between 3 and 6, defined earlier in the code. names is a string array that holds the names of the rooms, also defined earlier in my program.

Now first of all, this code doesn't compile. There is an issue with the line

   connections[j] = names[randomRoom];

I can't seem to figure out why this assignment statement doesn't work either. I am pretty new to C (I'm mostly experienced with Java) so I can't figure it out. The error I get is: incompatible types when assigning to type char[7] from type char *

I should mention, this is all in one function defined as:

    void createRooms(FILE *fp)

I know there are probably more efficient ways to do this, but at this point I'm just trying to get the code working and deal with efficiency later.

I've done a ton of googling and am honestly beating my head against the wall right now. Any help would be greatly appreciated. If there's any more code I should post or any other information let me know.