dimanche 24 janvier 2016

Formula used for function rand() in c++

I want to know what is the formula used for generating random numbers using the rand() function in C++. At first I though it would return random numbers every time, but it does not.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    srand(9865);
    cout << rand() << "\n";
    return 0;
}

Here I thought that, maybe because of given seed number and that unknown formula, it will be showing same number.

But, when i removed "srand(9865);" and execute several times it is showing only "41" as output. Please explain me what is all going on here.




Insert all values together with random values

I'm trying to insert data into 3 columns from another table T2. Insert random values from T2.DATA1 to T1.DATA1, and all values from T2 to columns DATA2 and DATA3. I have tried this query :

INSERT INTO T1 (DATA1, DATA2, DATA3)
SELECT (SELECT DATA1 FROM
(SELECT DATA1 FROM T2
ORDER BY dbms_random.value)
WHERE rownum = 1), DATA2, DATA3
FROM T2;

it return correct values to DATA2 and DATA3, but in DATA1 I have one generaated random value in all rows. How can I modify this query to have random value in every row ? T2 contains around 3000 rows.




samedi 23 janvier 2016

How can I get my javascript function randomly select one function(slideshow), execute it, then repeat?

I have a javascript code for replacing a html <div> that contains an image in it, so it becomes like a slide show. I have six of these slideshows on the page, and I need them to randomly change maybe switch image 3 then 6 then 2 and ect.

$(document).ready(function(){
     SlideShow6();
});

function SlideShow6() {
    $('#slideshow6 > div:gt(0)').hide();

    setInterval(function () {
        $('#slideshow6 > div:first')
        .fadeOut(0)
        .next()
        .fadeIn(0)
        .end()
        .appendTo('#slideshow6');
    }, 0000);
}

The time over here at the end is 0000 (0)seconds. Becuase I need the timer to have a 2000 (2)second pause before picking a random slide to slide. I tried my script with this:

var funcs = [];

funcs[0] = function() {
    $('#slideshow6 > div:gt(0)').hide();

    setInterval(function () {
        $('#slideshow6 > div:first')
        .fadeOut(0)
        .next()
        .fadeIn(0)
        .end()
        .appendTo('#slideshow6');
    }, 0000);
}
funcs[1] = function() {
    $('#slideshow5 > div:gt(0)').hide();

    setInterval(function () {
        $('#slideshow5 > div:first')
        .fadeOut(0)
        .next()
        .fadeIn(0)
        .end()
        .appendTo('#slideshow5');
    }, 0000);
}

// and so on six times... then...

$(document).ready(function(){
    var rand = parseInt(Math.random()*funcs.length);  
    funcs[rand]();
    setTimeout(arguments.callee, 2000);
});

It becomes really wierd, it selects my functions randomly but it executes the slideshow a unlimited number of times and before soon all six of them are on and going. Maybe because of the 0000? I need them to switch image one by one.




Put image on div randomly using javascript

I am making matching game that images in left and right are different, and users have to find(I put one more image in left side). My problem is that I cannot call image on where I want. What should I have to fix? Thank you.

<!DOCTYPE html>
<html>
<head>
<style>
 img {
   position:absolute;
   }
 div {
   position: absolute;
   width: 500px;
   height:500px;
   }
 #leftside {
   float: left;
   }
 #rightside {
   float:right;
   left: 500px;
   border-left: 1px solid black
    }
</style>
<script>
 var numberOfFaces(5);
 var theLeftSide =document.getElementById("leftSide");

 function generateFaces(){
var createElement("img");
var position=Math.floor(Math.random()*500);
img.src='http://ift.tt/1GzZEsa';
 img.id='smileImage';
 createElement.setAttribute("height",position);
 createElement.setAttribute("width",position);
 document.getElementById('leftSide').appendChild(img);
  } 

  </script>
  </head>
  <body>
   <h1>Matching Game</h1>
    <p>Click on the extra smiling face on the left.</p>
    <div id="leftside"></div>
    <div id="rightside"></div>
    </body>
   </html>




Python guessing game keeps return same output

I am trying to create a python function that makes you guess a number 0 - 100. Every time I run the code, it always prints lower, even when I put the lowest number possible(0). How can I prevent this and have my code run correctly.

Here is my code:

from random import *
z = False

def whle(i):
    x = raw_input("Guess My Number 1 - 100")
    if x < i:
        print "Higher"
        q = False
        z = True
    elif x > i:
        print "Lower"
        q = False
        z = True
    elif x == i:
        print "Correct!"
        q = True
        z = False
    else:
        print "Guess My Number You Goofy Goose!"
        q = False
        z = True
def number():
    y = randrange(0,101)
    q = False
    while q == False:
       whle(y)
while z == False:
    number()




Modifying rand() ranges c++

I'm trying to make a guessing game program where the user thinks of a number between 1 and 100, and then the computer randomly generates(guesses) a number between 1 and 100. Then the user tells the computer if their number is higher(H), lower(L), or the same(Y) as the guessed number. The problem I am having is changing the range that the computer generates its guess in.

So let's say my number is 43 and the computer generates 15 as its guess. I would input H, indicating that my number is higher than its guess. The computer then changes the parameters to generate a number between 16 and 100.

int high = 100, low = 1;
char answer;

Setting my variables.

srand(time(0));

I have this to truly randomize the generation.

int guess = rand() % high + low;
cout << "Is your number " << guess << "?\n";
cin >> answer;

Generate the number, ask if the number is what I'm thinking of, and then ask me to answer if it is higher, lower, or correct.

Then the segment that checks the answer and determines how to change the range of the randomly generated number.

switch (answer)
    {
    case 'H':
        low = (guess + 1);
        high = (high - guess);
        break;
    case 'L':
        high = guess;
        break;
    default:
        break;
    }

I guess what I'm not understanding is how rand() truly works. Like I understand the whole

rand() % 100 //range of 0 and 99
rand() % 100 + 1 // range of 1 and 100

But I want to know how I can change the parameters so that I get

rand() % max + min // range of min and max

while I constantly change and update the values for max and min.




PHP random image generator from Imgur

I'm looking to create a website that each time a person directs to it, it will grab a random image from an album and display it without going elsewhere. I am aware I could use the Imgur API to make this easier but I don't know where to start. Does anybody know how I may be able to do this?