google-CSE的结果是随机的

时间:2015-12-12 15:37:38

标签: java search google-custom-search

我正在使用谷歌的CSE创建谷歌搜索引擎。我每次搜索查询时都试图获得一个随机结果。例如:当我搜索"咖啡"引擎从所有"咖啡结果"中随机返回一个结果。

是否有人可以帮我随机制作结果?

谢谢!

1 个答案:

答案 0 :(得分:0)

  • 如果您正在使用 Linux ,则可以从/dev/random读取,因为它将包含随机流。
  • 对于 Windows ,请使用the %RANDOM% environment variable
  • JavaScript 有一个方法Math.random();(请参阅here)。
  • PHP mixed array_rand ( array $array [, int $num = 1 ] ) int rand ( int $min , int $max )pick a random element from an array new Random().nextInt(number_of_results + 1);
  • Java 可以使用chosen_result = results_array[random_num];
  • 生成随机数

对结果进行编号(即将它们存储在数组中)并使用随机数来选择结果(// // File.swift // Reading Logs // // Created by Andrew on 12/8/15. // Copyright © 2015 Wilson Apps for Education. All rights reserved. // import UIKit class UserLogin { var loginAlert = UIAlertView() var user: String = "" var pass: String = "" func checkLogin() -> Bool{ let defaults = NSUserDefaults.standardUserDefaults() let stat = defaults.valueForKey("loggedIn") if(String(stat!) == "0"){ return false }else{ return true } } func logout(){ let defaults = NSUserDefaults.standardUserDefaults() defaults.setValue("0", forKey: "loggedIn") defaults.setValue("", forKey: "logKey") defaults.setValue("0", forKey: "userKey") } func login(username username: String, password: String, completion: (result: String) -> Void){ self.user = username self.pass = password let url = "http://www.wilsonfamily5.org/rlog/checkLogin.php?u=\(username)&p=\(password)" let nsUrl = NSURL(string:url) let nsUrlRequest = NSURLRequest(URL: nsUrl!) NSURLSession.sharedSession().dataTaskWithRequest(nsUrlRequest){ (data, response, error) in guard let data = data, let contents = NSString(data:data, encoding:NSUTF8StringEncoding) else { return } dispatch_async(dispatch_get_main_queue(), { () -> Void in if(contents as String == "0"){ self.loginAlert = UIAlertView(title: "No Account Found", message: "We did not find an account matching that criterea. Do you want us to create you an account?", delegate:nil, cancelButtonTitle: "No") self.loginAlert.addButtonWithTitle("Yes") self.loginAlert.show() }else{ let defaults = NSUserDefaults.standardUserDefaults() defaults.setValue(contents as String, forKey: "userKey") defaults.setValue("1", forKey: "loggedIn") completion(result: "1") } }) }.resume() } func register(username: String, password: String){ let url = "http://www.wilsonfamily5.org/rlog/newAccount.php?u=\(username)&p=\(password)" let nsUrl = NSURL(string:url) let nsUrlRequest = NSURLRequest(URL: nsUrl!) NSURLSession.sharedSession().dataTaskWithRequest(nsUrlRequest){ (data, response, error) in guard let data = data, let contents = NSString(data:data, encoding:NSUTF8StringEncoding) else { return } dispatch_async(dispatch_get_main_queue(), { () -> Void in let defaults = NSUserDefaults.standardUserDefaults() defaults.setValue(contents as String, forKey: "userKey") defaults.setValue("1", forKey: "loggedIn") }) }.resume() } func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { print("ButtonClicked") if(buttonIndex == 1){ print("1ButtonClicked") register(user, password: pass) } } }

如果这不符合您的需求,您需要提供更多信息,例如您为此使用的编程语言。