dimanche 2 octobre 2016

Java Random generating all 1's

this has stumped me. why would this return a string of all 1's? I do believe that it has something to do with the seed value. when that is changed the numbers become much more diverse.

Random random = new Random(441287210L);

for (int i = 0; i < 10; i++)
   System.out.print(random.nextInt(10) + " ");

any ideas?




how to change distribution of rng class in ns-2

RNG Class I'm using rng class in ns-2 to generate a random number with Gaussian distribution using normal() function. But I don't know how it works. Now I want to change the distribution of it. Please help me.




Random item from nested dictionary [duplicate]

This question already has an answer here:

I'm trying to choose a random item from this nested dictionary

import random
swords = {
    'Short Sword': {'attack': 3, 'defence': 1, 'price': 20},
    'Sword': {'attack': 7, 'defence': 1, 'price': 35},
    'Long Sword': {'attack': 10, 'defence': 1, 'price': 50},
    'Broad Sword': {'attack': 14, 'defence': 1, 'price': 75}
}

And if I use

randsword = random.choice(swords.items())

I get this error

TypeError: 'dict_keys' object does not support indexing 




c++ - rand() between number which are multiple

So I would like to have a random number that is a multiple of 32, so, 32, 64, 96, 128...

rand() return a number which is not always a multiple of 32 of course. How could I proceed ?




Filling a plane with random points

I would like to fill a plane with randomly placed points, check whether any of them overlap (and if they do, move one of them to empty place) and then calculate the average distance between them. Later I plan on extending that to 3D so that it is kind of having particles in a box. I know there must be better ways of doing it but here's what I came up with. For placing random points in a plane:

int pos[NUMBER][2];    /* Creates an array of NUMBER amount of points with x and y coordinate */
int a, b;
srand( time(NULL) );
for(a=0;a<NUMBER;a++)
      for(b=0;b<2;b++)
         pos[a][b]=rand()%11; /* Using modulus is random enough for now */

The next stage is finding points that over lap:

    for(a=0;a<NUMBER-1;a++)
      for(b=a+1;b<NUMBER;b++)
        if( pos[a][0] == pos[b][0] && pos[a][1] == pos[b][1])
           printf("These points overlap:\t", pos[a][0], pos[a][1]);

Now when I identify which points overlap I have to move one of them, but when I do the point in new position might overlap with one of the earlier ones. Is there any accepted way of solving this problem? One way is infinite while(true) loop with breaking condition but that seems very inefficient especially when system gets dense. Thank you!




samedi 1 octobre 2016

Extract last value from an integer and convert into a character in C Language

I'm working on a program that takes a seed number and produces a series of random numbers whose last digits form a lottery number. In one example, the last digits of the first four numbers are used (3 4 3 1) but in another the last digits of only the first three are used (5 7 2), so I'm converting the integers to characters to utilize the space in the ASCII table(so that can be the fourth digit).

My first major dead end is figuring out how to extract those last integer values and turn them into characters. So I'd really appreciate help with that. Keep in mind: I can't use if/else statements or relational operators. This program makes use of selection through calculation.




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");