How to get Random number from variable

Hello:


I have the following scenario:


Using NSFetchRequest i get a count of items returned as:

numberRecords = items.count


I want to get a random record based on the number of records in "numberRecords".

Now i can get a random value using : selector = Int.random(in: 0 ..< 355)

But how can I use "numberRecords" to get a numerical value?


Thanks.

Answered by Claude31 in 399373022

From what I understand, there is no easy and robust way to fect a random element directly.

See detailed discussion here

https://stackoverflow.com/questions/2830533/iphone-os-fetching-a-random-entity-instance-using-nspredicate-nsfetchrequest-an


Unless to select a search key randomly and then fetch.

If the key includes a sequential number, that shouyd be easy

https://stackoverflow.com/questions/34917149/how-to-randomly-choose-an-element-from-coredata-swift


But, why not fetch all results and then do the random pick ?


Note: looks like you expect fetch to paly as an agent that executes in CoreData to fetch a random item.

AFAIK, fetch has not such a capability, it does "only" pattern matching.

just write:


let x = items.count

let r = Int.random(in: 0..<x)

print(r)



Is it what you are looking for ?

You can also access directly a random element in the array


let randomItem = items.randomElement()


if let item = myArray.randomElement() {     // randomElement is an optional
    print(item)
}

I get the construction for the random value -- let randomItem = items.randomElement()


but I'm not getting a successful construction of fetchRequest using "randomItem"


fetchRequest.predicate = NSPredicate(format: )

Accepted Answer

From what I understand, there is no easy and robust way to fect a random element directly.

See detailed discussion here

https://stackoverflow.com/questions/2830533/iphone-os-fetching-a-random-entity-instance-using-nspredicate-nsfetchrequest-an


Unless to select a search key randomly and then fetch.

If the key includes a sequential number, that shouyd be easy

https://stackoverflow.com/questions/34917149/how-to-randomly-choose-an-element-from-coredata-swift


But, why not fetch all results and then do the random pick ?


Note: looks like you expect fetch to paly as an agent that executes in CoreData to fetch a random item.

AFAIK, fetch has not such a capability, it does "only" pattern matching.

Hello Claude3:

You asked: But, why not fetch all results and then do the random pick ?


That is actually what I am doing. However after I have the results, I must pick a random item from that results. The problem is I don't have a sequential index for any of the columns from which to construct the predicate:


selector = Int.random(in: 0 ..< x)
fetchRequest.predicate = NSPredicate(format: "??? == %@", selector) // ??? should be a column with a set of numbers between "0...<x.

But do the other way:


fetch all object in the array (you don't tell if it is for a specific entity)

https://stackoverflow.com/questions/51946949/swift-core-data-how-to-fetch-all-items-in-array


let fetchRequest = NSFetchRequest(entityName: "MyEntity")

 do {
  let results = try managedObjectContext.executeFetchRequest(fetchRequest)
  let items = results as! [ItemType]

  } catch let error as NSError {
  print("Could not fetch \(error)”)
 }


Then pick a random

let randomItem = items.randomElement()

Hello Calude:

This is still returning many records.


let randomItem = items.randomElement()


I decided on another plan:


I created indexed subsets of my data and use the following:

selector = Int.random(in: 1 ..< ???) // inserting the numbeer records in the subset


then fetch so:


fetchRequest.predicate = NSPredicate(format: "qid == %@", selector as NSNumber)


Thanks for the insights that led me to asuccessful resolution.

How to get Random number from variable
 
 
Q