0

Code first generates a random between 0-8, assigning it to var n. Then a 2nd randomNumber Generator func is looped n amount of times to generate n amount of ints between 0 and 10, all having different probabilities of occurring and ultimately put into an array. What I want is for none of those 10 possible numbers to repeat, so once one is chosen it can no longer be chosen by the other n-1 times the func is run. I'm thinking a repeat-while-loop or an if-statement or something involving an index but I don't know exactly how, nor within what brackets. Thanks for any help! Some whisper this is the most challenging and intelligence demanding coding conundrum on earth. Challenge Accepted?

import UIKit

let n = Int(arc4random_uniform(8))

var a:Double = 0.2
var b:Double = 0.3
var c:Double = 0.2
var d:Double = 0.3
var e:Double = 0.2
var f:Double = 0.1
var g:Double = 0.2
var h:Double = 0.4
var i:Double = 0.2
var j:Double = 0.2
var k: [Int] = []

for _ in 0...n {
    func randomNumber(probabilities: [Double]) -> Int {
        let sum = probabilities.reduce(0, +)
        let rnd = sum * Double(arc4random_uniform(UInt32.max)) / Double(UInt32.max)
        var accum = 0.0
        for (i, p) in probabilities.enumerated() {
            accum += p
            if rnd < accum {
                return i
            }}
        return (probabilities.count - 1)
    }
    k.append(randomNumber(probabilities: [a, b, c, d, e, f, g, h, i, j]))
}
print(k)
5
  • i can tell you the psuedo code if you want OR a proper python code Commented Jan 24, 2018 at 11:33
  • Yes please!.... Commented Jan 24, 2018 at 11:34
  • psuedo code..... Commented Jan 24, 2018 at 11:39
  • Your example has a problem. Your ten numbers (a to j) only have four values among them: 0.1, 0.2, 0.3, 0.4. Once you have selected those four values then the fifth selection must be a repeat. It is impossible to get up to 8 numbers with your conditions. Commented Jan 24, 2018 at 12:26
  • No because those values become the probabilities of var occurring. When ran, what comes out are ints 0-10 Commented Jan 25, 2018 at 10:05

2 Answers 2

1

pseudo Code -

1)generate a number between 1-8
    n
2)take empty array
    arr[]
3)loop from 0 to n
    1) generate a random no
        temp
    2) check if it is there in arr
            > if it is there in arr, generate another
    3) when you get a number which is not there in arr, insert it

This is a python code

import random

n = random.randint(1,8)
arr = []
print(n)
for each in range(n):
    temp = random.randint(1, 10)
    while temp in arr:
        temp = random.randint(1, 10)
        print(temp)
    arr.append(temp)
print(arr)

Check code example here

Sign up to request clarification or add additional context in comments.

Comments

1

Swift version of Ankush's answer -

 let n = arc4random_uniform(7) + 1
 var arr: [UInt32] = []
 for _ in 0 ... n {
    var temp = arc4random_uniform(9) + 1
    while arr.contains(temp) {
        temp = arc4random_uniform(9) + 1
    }
    print(temp)
    arr.append(temp)
 }
print(arr)

Hope this helps!

3 Comments

it does! Can I ask why you did arc4random_uniform(7) + 1 instead of just (8)?
Because arc4random_uniform(7) generates random number between 0 to 7
Could you help me. I dont understand how to intertwine both methods. Mine got me the new array based of probabilities but they repeated, the one you gave doesn't repeat but doesn't incorporate the probabilities

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.