lundi 2 janvier 2017

Generate random number in same range for multiple varaibles

I am trying to find out how to generate a random number in the same range for multiple variables easily instead of typing out the same thing many times.

For example:

num1 = rnd.Next(1, 10);
num2 = rnd.Next(1, 10);

Is there a more simple way to do that in a single line?




Random number with probability

I am trying to generate a random number and have a probability of x% to get closer of the max value.

Problem is that I have no idea how to do this, unless I make some steps?

Let's say I'll generate a random number between 1 and 3000.

$number = mt_rand(1, 3000)

This will return 2700 for example. I would like it to be closer to 1 than 3000.

How should I implement a function that will only have a 10% chance to get near 3000?




How should I properly seed srand()

I have these snippets of code:

char letrarand()
{
    time_t t;
    srand(time(NULL));                                                  
    char randomletter = 'A' + (rand() % 26);
    return randomletter;
}

char criameses()
{
    int contador = 0;
    for (contador = 0; contador < 31;++ contador) 
        {
            *(pjaneiro + contador) = letrarand();
        }

Where pjaneiro is a pointer to a 31 element array. Theoretically this works and would assign a random letter to each element of the array, but as it turns out it always assigns the same letter.

My best guess as to why this happens is that the time() functions is only accurate to a second so the seed to the random number generator is always the same because my computer executes the code in less than one second. Is there a way for me to better seed the random number generator?




C# replacing "Random" text with string (wildcard)

i want to replace this string: "color":"efea27", with "color":"0000ff", but the "efea27" could be anything (its random). i want to change that to what the user inputs. for example 0000ff how do i do that when i dont know what is between the color "" ?

Thanks! -shinevision




How to format a long python list into a FORTRAN multi-line array?

I have a problem. I am programming a math calculation program in FORTRAN. In there I have to initialize an array of random values. These values have to be normally distributed with a mean of 0 and a standard deviation of 1.

I did the following script in python 2.7 to generate 900 of such values.

import numpy as np

mu, sigma = 0, 1.0
list = []

i = 0
while i < 901:
    s = np.random.normal(mu, sigma, None)
    list.append(format(s, '.3f'))
    i += 1

print list

This returns this list:

['-1.403', '-1.498', '0.573', '-0.056', '-0.226', '-0.514', ..., ]

The problem is that I can't just copy this into my FORTRAN code because the values there are written in the followint way:

DATA STR / 0.978,  -0.52 ,  -0.368,   1.69 ,  &   !Giving random values. Temperary solution for
          -1.48 ,   0.985,   1.475,  -0.098,  &   !random number generating, based on the normal law
          -1.633,   2.399,   0.261,  -1.883,  &
          -0.181,   1.675,  -0.324,  -1.029,  &
          -0.185,   0.004,  -0.101,  -1.187,  &
          -0.007,   1.27 ,   0.568,  -1.27 ,  &
           ... &
           ... &
           ... &

           /

Meaning that I have to format the python list into something like:

NUM1, NUM2, NUM3, NUM4, &
XXXX, XXXX, XXXX, XXXX, &
...
...
...

How would I go about doing this?

Thanks.




Unexpected EOF while Parsing in Python Random Sentence Generator

I am running Python 2.7.12 with no extensions. This is my code, and I am getting an Unexpected EOF while parsing.

import random
import sys
import time
print("Welcome to the weird sentence generator.")
wantto = True
print("Press enter to generate a weird sentence. Press Q to quit.")
while True:    
    senttype = random.randint(1,5)
    VERBS = ["dying", "farting","eating","pooping","pooing","drinking","running","walking","dancing","singing","learning","burping","digesting","fighting","licking","flying","smoking","mating","reproducing","playing","thinking","peeing","typing","programming","sitting","standing","slipping","ice-skating","skating","riding","murdering","killing","snoring","watching","looking","sprinting","jogging","opening","closing","calling","drawing","painting","washing","cleaning","cooking","kissing","snogging","showering","bathing","lifting","listening","conjuring","writing","composing","socializing","talking","lip-syncing to music.ly","being annoying","wrestling","practicing martial art","meeting people","skateboarding","riding a bike","proposing to his girlfriend","trying to get good at Game of War because he kept getting rekt by TheLegend27","clicking stuff on his PC","clicking his tongue","whistling","clapping","helping someone kill themself","failing to think of examples for this random sentence program"]
verb = random.choice(VERBS)
VERBSTWO = ["die","fart","eat","poop","poo","drink","run","walk","dance","sing","learn","flirt","digest","fart","eat","fly","burp","dig","smoke","lick","snore","kiss","snog","vomit","throw up","play","think","pee","type","arrest","program","sit","stand","lie down","slip","ice-skate","skate","skateboard","ride","murder","kill","slaughter","snore","watch","look","sprint","jog","open","close","call","drag","draw","paint","photograph","wash","clean","cook","bathe","solve"]
NOUNS = ["pig","cow","chicken","camel","man","woman","girl","boy","mouse","house","box","computer","fox","omelette","egg","sandwich","mouse pad","remote control","TV remote","screen","frame","statue","painting","wall","floor","ceiling","window","table","chair","TV","xBox 360","Wii","Playstation 1","Playstation 2","Playstation 3","Playstation 4","xBox 1","Nintendo DS","Game Boy","Nintendo 3DS","charger","phone","telephone","iPhone","iPad","iPod","flower","tree","bush","president","prime minister","baby","mirror","cupboard","wardrobe","walrus","goat","koala","sea lion","fish","goldfish","DVD","DVD player","USB","bed","bedroom","toilet","bathroom","living room","room","kitchen","oven","fridge","freezer","gate","snake","python","document","sentence","windowsill","shower","dining room","napkin","fork","knife","spoon","sugar cube","sugar","salt","potato","carrot","cucumber","tomato","soup","vinegar","crisp","chip","French fry","egg","pillow","blanket","matress","sheet","file","user","rapper","singer","runner","builder","zombie","wizard","engineer","monkey","gorilla","baboon","skeleton","tiger","pop star"]
noun = random.choice(NOUNS)
ADVERBS = ["slowly","quickly","stupidly","cleverly","harshly","horribly","beautifully","skillfully","easily","difficultly","nicely","pleasantly","surprisingly","prettily","artfully","craftily","hungrily","thirstily","greedily","selfishly","unselfishly","kindly","generously","randomly","idiotically","periodically","logically","illogically","controversially","sickeningly","poorly"]
adverb = random.choice(ADVERBS)
ADJECTIVES = ["fat","thin","weird","strange","normal","average","random","legendary","horrible","vile","cruel","ugly","pretty","beautiful","disgusting","pleasant","surprising","nice","artful","pretty","crafty","hungry","thirsty","greedy","selfish","unselfish","kind","generous","thorough","idiotic","stupid","obese","logical","illogical","controversial","sickening","poor","dumb"]
adjective = random.choice(ADJECTIVES)
adjective2 = random.choice(ADJECTIVES)
noun2 = random.choice(NOUNS)
COLOURS = ["blue","green","maroon","yellow","pink","purple","white","black","brown","hazel","red","orange","turquoise","peach","tan","chestnut","chocolate"]
colour = random.choice(COLOURS)
verb2 = random.choice(VERBSTWO)
what = input()
if senttype == 1:
    print("The",noun,"was",adverb,verb,"in the",adjective,"park, but the", adjective2,colour,noun2,"wanted him to", verb2+".")
if senttype == 2:
    print("My favourite",noun,"is the",adjective,colour,"one",adverb,verb,"in a",noun2+"!")
if senttype == 3:
    print("I",verb2,"with a",adjective,noun,"frequently and",adverb+".")
if senttype == 4:
    print("Although",verb,"is",adjective+", the",noun,"was too",adjective2,"to",verb2,"it.")
if senttype == 5:
    print("The",adjective,colour,noun,"is good at",verb,"and likes to",adverb,verb2,"every day!")
if what =="Q" or what == "q":
    sys.exit()

Thanks if you could help, and yes, I will 'accept' answers if you want some reputation.




creating a social network of random walking turtles

i'm fairly new here and new to netlogo so i will try my best to explain what i need help with.

I'm looking to create a small world of random walking turtles and each trutles should have a label so it show how many time they have encountered another turtle.

thanks for any help in advance.