dimanche 1 octobre 2017

Generating randomized objects in a loop in c++

I have a person object class which is made up of name, address, and phone number object classes. The name, address, and phone number are randomly generated.

The user selects how many random people to generate and a function is called that generates them with a loop and stores them in a vector:

vector<P> pV;  //vector of people

for(int i = 0; i < num; i++){
    P x; //create new person object x
    pV.push_back(x) //add to vector of people
    cout << pV[i].getName();  //print for text
}

The problem is, all the objects generated are identical (same name, address, etc). If I generate a new list during the same session, a person with different information gets generated but they'll still all be identical.

I know this can happen in Java if you use static when randomizing... I'm not sure of the equivalent in C++.

Any suggestions?

Thanks




Stopped working

I worked up this code with dummy images and all the same link. When I put in the real images and links (which also made me increase the number of elements in the arrays), the code stopped shuffling. It runs the shuffle code, but the images aren't going up in random order each time like they were previously. They are just staying in the order they are defined in. I need to get this fixed ASAP. Any ideas?

Javascript Code

    var $sectionOne = $('#sectionOne');
    var $sectionTwo = $('#sectionTwo');
    var $sectionThree = $('#sectionThree');
    var $sectionFour = $('#sectionFour');

    var adOne = new Array();
adOne[0] = '<a target="_blank" href="http://ift.tt/1kH012g"><img src="./resources/adimages/October/blessings.png"/></a>';
adOne[1] = '<a target="_blank" href="http://mummydeals.org/"><img src="./resources/adimages/October/clair.jpg"/></a>';
adOne[2] = '<a target="_blank" href="http://ift.tt/2g2SumM"><img src="./resources/adimages/October/onlinetrade.jpg"/></a>';
adOne[3] = '<a target="_blank" href="http://ift.tt/2hGNYhA"><img src="./resources/adimages/October/villageshop.png"/></a>';
adOne[4] = '<a target="_blank" href="./resources/adimages/October/starlightmusic.png"><img src="./resources/adimages/October/rsz_starlightmusic.png"/></a>';
adOne[5] = '<a target="_blank" href="http://ift.tt/2g0wRU7"><img src="./resources/adimages/250yah.png"/></a>';

var adTwo = new Array();
adTwo[0] = '<a target="_blank" href="http://ift.tt/2hEZYjH"><img src="./resources/adimages/October/honeydew.png"/></a>';
adTwo[1] = '<a target="_blank" href="http://ift.tt/2g2SuTO"><img src="./resources/adimages/October/jennappel.jpg"/></a>';
adTwo[2] = '<a target="_blank" href="http://ift.tt/2g0wRU7"><img src="./resources/adimages/250-125yah.png"/></a>';

var adThree = new Array();
adThree[0] = '<a target="_blank" href="http://ift.tt/2hG62Zl"><img src="./resources/adimages/October/acoustic.jpg"/></a>';
adThree[1] = '<a target="_blank" href="http://ift.tt/2g2SvqQ"><img src="./resources/adimages/125_kingcover.jpg"/></a>';
adThree[2] = '<a target="_blank" href="http://ift.tt/2g0wRU7"><img src="./resources/adimages/125yah.png"/></a>';

var adFour = new Array();
adFour[0] = '<a target="_blank" href="http://ift.tt/2cX3YID">The Usual Mayhem</a>';
adFour[1] = '<a target="_blank" href="http://ift.tt/2g2SvXS">Blooming With Joy | Tea Blends & Gifts</a>';
adFour[2] = '<a target="_blank" href="http://ift.tt/2hECo6t">Buy or sell AVON online</a>';
adFour[3] = '<a target="_blank" href="http://ift.tt/2g2B6OW">The Boondocks Wentworth</a>';
adFour[4] = '<a target="_blank" href="http://ift.tt/2hGJdVa">Thrive Well with Essential Oils!</a>';
adFour[5] = '<a target="_blank" href="http://ift.tt/2g2PczX">Perfectly Posh... Because you deserve it!</a>';
adFour[6] = '<a target="_blank" href="http://ift.tt/2hFUHZa">Whole Family Plant Based Wellness</a>';
adFour[7] = '<a target="_blank" href="http://ift.tt/2g2SwuU">Linen World and More</a>';
adFour[8] = '<a target="_blank" href="http://ift.tt/2hGA4fj">All your legging needs- sizes 2T-26W</a>';
adFour[9] = '<a target="_blank" href="http://ift.tt/2g0wRU7">Link to your blog or business. $10</a>';

$(document).ready(function() {

//Shuffle one
adOne.shuffle = function() {
    var input = this;

    for (var i = adOne.length-1; i >=0; i--) {
        var randomIndex = Math.floor(Math.random()*(i+1));
        var itemAtIndex = adOne[randomIndex][0];
            var itemAtSecond = adOne[randomIndex][1];

        input[randomIndex][0] = input[i][0];
            input[randomIndex][1] = input[i][1];
        input[i][0] = itemAtIndex;
            input[i][1] = itemAtSecond;
    }
    return input;
}
//end of shuffleOne function

//Shuffle two
  adTwo.shuffle = function() {
      var input = this;

      for (var i = adTwo.length-1; i >=0; i--) {
          var randomIndex = Math.floor(Math.random()*(i+1));
          var itemAtIndex = adTwo[randomIndex][0];
            var itemAtSecond = adTwo[randomIndex][1];

          input[randomIndex][0] = input[i][0];
            input[randomIndex][1] = input[i][1];
          input[i][0] = itemAtIndex;
            input[i][1] = itemAtSecond;
      }
      return input;
  }
  //end of shuffleTwo function

  //Shuffle three
    adThree.shuffle = function() {
        var input = this;

        for (var i = adThree.length-1; i >=0; i--) {
            var randomIndex = Math.floor(Math.random()*(i+1));
            var itemAtIndex = adThree[randomIndex][0];
                var itemAtSecond = adThree[randomIndex][1];

            input[randomIndex][0] = input[i][0];
                input[randomIndex][1] = input[i][1];
            input[i][0] = itemAtIndex;
                input[i][1] = itemAtSecond;
        }
        return input;
    }
    //end of shuffleThree function

    //Shuffle four
      adFour.shuffle = function() {
          var input = this;

          for (var i = adFour.length-1; i >=0; i--) {
              var randomIndex = Math.floor(Math.random()*(i+1));
              var itemAtIndex = adFour[randomIndex][0];
                var itemAtSecond = adFour[randomIndex][1];

              input[randomIndex][0] = input[i][0];
                input[randomIndex][1] = input[i][1];
              input[i][0] = itemAtIndex;
                input[i][1] = itemAtSecond;
          }
          return input;
      }
      //end of shuffleFour function
adOne.shuffle();
adTwo.shuffle();
adThree.shuffle();
adFour.shuffle();

for (i=0; i < adOne.length; i++) {
$sectionOne.append('<div class="one"> ' + adOne[i] + '</div>');
};
//end of placeAdsOne function

placeAdsTwo = function(adTwo, $sectionTwo, i) {
$adInSectionTwo = $('<div class="two"> ' + adTwo[i] + '</div>');
$sectionTwo.append($adInSectionTwo);
};
//end of placeAdsTwo function

placeAdsThree = function(adThree, $sectionThree, i) {
$adInSectionThree = $('<div class="three"> ' + adThree[i] + '</div>');
$sectionThree.append($adInSectionThree);
};
//end of placeAdsThree function

placeAdsFour = function(adFour, $sectionFour, i) {
$adInSectionFour = $('<div class="four"> ' + adFour[i] + '</div>');
$sectionFour.append($adInSectionFour);
};
//end of placeAdsFour function


for (i=0; i < adTwo.length; i++) {
 placeAdsTwo(adTwo, $sectionTwo, i);
};


for (i=0; i < adThree.length; i++) {
 placeAdsThree(adThree, $sectionThree, i);
};


for (i=0; i < adFour.length; i++) {
 placeAdsFour(adFour, $sectionFour, i);
};

});
//end of document ready




How to randomize the output location of multiple images in Python 3

I should admit upfront that I'm learning python as I go and using google as my primary teacher. I appreciate any help or advise that you all can offer!

I am writing a program that plays a sound and then gives the user four image options to choose from. The issue I have encountered is the randomization of the locations of the four images. I have written a program that will randomize the order of 4 standard text options and then display the correct image after the fact and I have wiritten a program that will display four random images but the correct image is always the 4th image. Can I use the random function or some sort of shuffle function to rearrange the order of the displayed images each time?

The code for the program that displays the images in the same position is below. Note, my next step is to get the program to recognize when the right button is pressed, right now it just re-displays the same image, but that's not my question this time. Thanks for the help!

import os, random
from random import choice
import winsound
import time
from tkinter import *

master=Tk()

Imagepath = "C:\\LearnArabic\\alphabet\\Image\\"         
Soundpath = "C:\\LearnArabic\\alphabet\\Sound\\"
Letter = random.choice(os.listdir(Soundpath))

answer1 = PhotoImage(file=Imagepath+((random.choice(os.listdir(Imagepath)).rsplit(".", 1)[0])+ ".png"))
answer2 = PhotoImage(file=Imagepath+((random.choice(os.listdir(Imagepath)).rsplit(".", 1)[0])+ ".png"))
answer3 = PhotoImage(file=Imagepath+((random.choice(os.listdir(Imagepath)).rsplit(".", 1)[0])+ ".png"))
correctanswer = PhotoImage(file=(Imagepath+((Letter.rsplit(".", 1)[0])+ ".png")))

winsound.PlaySound((Soundpath)+ (Letter), winsound.SND_FILENAME)

def button1():
    label = Label(master, image=answer1, anchor = S)
    label.image = answer1
    label.place(x=50, y=100)


def button2():
    label = Label(master, image=answer2, anchor = S)
    label.image = answer2
    label.place(x=100, y=100)

def button3():
    label = Label(master, image=answer3, anchor = S)
    label.image = answer3
    label.place(x=50, y=150)

def button4():
    label = Label(master, image=correctanswer, anchor = S)
    label.image = correctanswer
    label.place(x=100, y=150)

button1 = Button(master, image=answer1, command= button1)
button1.grid(row =2, column=2)

button2 = Button(master, image=answer2, command= button2)
button2.grid(row =3, column=2)

button3 = Button(master, image=answer3, command= button3)
button3.grid(row =2, column=3)

button4 = Button(master, image=correctanswer, command= button4)
button4.grid(row =3, column=3)




How to simulate Euclidean distances between two uniformly random points in N-dimensional hyper-cube?

How to use python to simulate Euclidean distances between two uniformly random points in N-dimensional hyper-cube?




cpu requirements for `x86intrin.h`?

Hi there I thought the minimum CPU requirements to run x86intrin.h is an intel 3th gen processor.

however when i run this code

_rdseed64_step(&temp2);

i get the following error

error: inlining failed in call to always_inline 'int _rdseed64_step(long long unsigned int*)': target specific option mismatch

_rdseed64_step (unsigned long long *__p)

I have set the flag to march=native and it only occurs when i am using my desktop, when i run on my laptop that is running a 6th gen processor it works fine.




doctests for randomly generated values

Given the following code:

defmodule Pullapi.Workout do                                                                                     
  import Pullapi.Numbers

  @moduledoc """                                                                                                 
  Functions that generate a workout representation                                                               
  """

  @doc """                                                                                                       
  Returns a pullup set defined by the number of `max_reps` a user can do, a `percentage`, and the                
  number of maximum additional or decremented reps, `rep_bound`.                                                 

  ## Examples                                                                                                    
  iex> Pullapi.Workout.pullup_set(20, 60, 5)                                                                     
  %{"Action" => "Pullups", "Units" => "14"}                                                                      
  """
  @spec pullup_set(integer, integer, integer) :: map()
  def pullup_set(max_reps, percentage, rep_bound) do
    median = max_reps * (percentage / 100)
    unit_range = Pullapi.Numbers.median_range(round(median), rep_bound)
    units = Enum.random(unit_range)

    %{"Action" => "Pullups", "Units" => "#{units}"}
  end
end

The doctest fails with:

  1) test doc at Pullapi.Workout.pullup_set/3 (1) (PullapiTest)
     test/pullapi_test.exs:4
     Doctest failed
     code: Pullapi.Workout.pullup_set(20, 60, 5) === %{"Action" => "Pullups", "Units" => "14"}
     left: %{"Action" => "Pullups", "Units" => "8"}
     stacktrace:
       lib/pullapi/workout.ex:13: Pullapi.Workout (module)

Is there a way of specifying that the "Units" value is randomly generated? It looks like I'm following the way Enum.random is doctested




Random.nextInt returns contiguous same-value integers (frequently)

I see results like the below when running Random.nextInt(3) 81 times:

200010202002112102222211012021020111220022001021101222222022210222220100000100010

Notice the large contiguous blocks: 000, 22222, 111, 222222, 222, 22222, 00000, 000.

Intuitively, the sequence doesn't seem naturally / "real-world coin flip" random.

For example, to achieve 6x contiguous 2s there's only a 0.4% chance (AFAIK) and for 5x contiguous values there's a 1.2% chance ... so it seems unlikely I should keep seeing patterns like this in the output.

Would this happen in the real-world with a 3-sided coin? Or is this an expected deviation from "true random" when using Java's Random.nextInt(exclusiveMax) method?