lundi 1 février 2016

Elm random list implementation

Elm standard library offers a function to generate random lists that works fine. By taking a look at its implementation, we can see that the list is constructed in a functionnal style, i.e. from the end to the beginning. When enough elements have been generated, the reversed list is returned:

if n < 1 then
    (List.reverse list, seed)

I'm wondering why do we need to reverse this list? Is it necessary to ensure "correct" randomness?




Python Creating a nested list with random module

Let's say I want to create a nested list with number sequence, and I want no repeated list sequence.

i.e. I would like something like this: [[1,2,3,4],[2,1,3,4],[4,2,3,1]] ,where the len of list depend on how many non repeated number sequence I could generate in limited attempt (four attempts in below case)

So here my first attempt:

import random
rng = random.Random()
bd =list(range(4))
i = 0
result =[]

while i <4:
    rng.shuffle(bd)
    if bd not in result:
        result.append(bd)
    i +=1
print(result)

I created the a list bd = [0,1,2,3] first before entering the while loop.

While running the above code, I got surprisingly only one element in my nested list every time.

Result: [[1, 2, 0, 3]]

But if I modify the code a little bit, it works as I expected. Here my modified code:

import random
rng = random.Random()
i = 0
result =[]

while i <4:
    bd =list(range(4))
    rng.shuffle(bd)
    if bd not in result:
        result.append(bd)
    i +=1
print(result)

Result: [[3, 2, 0, 1], [1, 0, 3, 2], [0, 1, 3, 2]]

What I do is just reset the list bd in every loop over again.

As per my understanding the list bd would be shuffled in every loop, so resetting bd to [0,1,2,3] should be meaningless. Could somebody explain to me why the modified code works? Thanks.




Getting a random CGFloat value from an array?

I have an array that holds CGFloat values and I am trying to figure out how I would go about randomising through this array to get a random x co-ordinate:

let xAxisSpawnLocations: [CGFloat] = [screenWidth + 150, screenWidth + 100, screenWidth + 50 , screenWidth - 50, screenWidth - 100, screenWidth - 150]  

because I have to put them into a line of code that will position an object on the map.

obstacle.position = CGPoint(x: ARRAY VALUE HERE, y: yAxisSpawnLocations[0])

I have tried looking up how to randomise through an array but it only has it for Int32 values.

Can some please provide and example on how to do this.

Here is the full code for reference if you need to look at it:

let xAxisSpawnLocations: [CGFloat] = [screenWidth + 150, screenWidth + 100, screenWidth + 50 , screenWidth - 50, screenWidth - 100, screenWidth - 150]
let yAxisSpawnLocations: [CGFloat] = [0]                          

        for xLocation in xAxisSpawnLocations {

            for (var i = 0; i < Int(numberOfObjectsInLevel); i++) {


            let obstacle:Object = Object()

            obstacle.theType = LevelType.road


            obstacle.createObject()
            addChild(obstacle)



            obstacle.position = CGPoint(x: xLocation, y: yAxisSpawnLocations[0])
            obstacle.zPosition = 9000
           // addChild(greenFrog)
        }
      }
    }

The xLocation needs to be replaced by the randomiser




Randomly accessing dictionary string values in a .plist file

I have created a simple dictionary:

    let quotes: NSArray = ["hello", "ola", "hi"]

Then I randomly access the strings in this dictionary with the following code:

    let range: UInt32 = UInt32(quotes.count)
    let randomNumber = Int(arc4random_uniform(range))
    let QuoteString = quotes.objectAtIndex(randomNumber)
    self.resultLabel.text = QuoteString as? String

Since I would ultimately like aloft of items in my dictionary, I should create a .plist file and called it "Quotes.plist" - it will be easier to manage a dictionary of many strings.

How can I write a simple code so as to randomly access strings within my .plist?




Solr: Random sort order after index version change

I am using "solr.RandomSortField" to sort search result randomly base on passed random seed. it is working fine and giving same order for same random seed key.

Issue is:

When index version get changed by creating/deleting doc using API at run time. Then random order also get changed for same key between newer and older version.

By Searching in google, i came to know that "RandomSortField" use index version so when index version get changed sort order also get change.

Is then any when i can exclude index version from random sort or pass index version to my query to search in older version instead of new updated version and get same order while paging search result.

Mostly getting issue in pagination whenever order change. get duplicate result after some page.




Get Random Number Between Given Range in Java [duplicate]

This question already has an answer here:

How to Get Random Number Between Given Range in Java if minimum is more than 1? for example if i want to get random number between 6 to 20. currently i am using this code

Random rand = new Random(); 
int  rnd = rand.nextInt(499) + 1;

it is working well, but if i change 1 to some high number for example 6, it will not work properly, i want to get random number that is between 6 and 20. is it possible using above function ?




Pick a random item from a list

I'm learning Javascript and i need some help. I have a list. I've tried to make a list, where you can, by the click of a button, get a random song from that list, but it doesn't seem to work. My list is down below, what am i doing wrong?

<!DOCKTYPE html>
<html>
<head>
</head>
<body>

<div>
  <button type="randomSong">Random Music</button>
  <input "randomSong" id="randomSong">
</div>

<script>

var song = Array("song1", "song2", "song3", "song4", "song5", "song6");

var randomSong = song[Math.floor(Math.random()*song.length)];

</script>

</body>
</html>