如何在Java中实现随机字生成器?

I was building an app to display random youtube videos. For the app, I needed an easy way to get a random english word. Suprisingly, finding an API which fit my requirements was harder than I expected. Finally, I ended up writing my own random word generator using Datamuse AP一世.

Why I couldn't use existing options?

我分析了很多API,但都不符合我的要求

  • Wordnik - Rich API with lots of documentation, but they were taking more than 7 days to send me an API key unless I donated money.
  • WordsAPI - Sleak website with straightforward random word API, but the free plan allowed only 2500 requests per day, which is a bit too low in my opinion.
  • random-word-api - Pretty good except that it generates a "random" word by choosing from a static list of words, which may result in words getting repeated frequently.
  • DataMuse API - This was most promising with 100,000 free requests per day, though they didn't have a straightforward random word api.

Finally, I decided to write my own random word generator java library using the DataMuse word-search API.

Writing my own random word generator

As explained before, Datamuse 一种PI was my best option. Sadly, instead of a random-word API they only had a word-search API. You make a REST API call to them with a topics parameter, and they send you back a list of words on those topics. The random word generator library which is just a wrapper around the Datamuse API. The library allows you to get a random word with a simple static method call as below

// Import the class
import com.github.dhiraj072.randomwordgenerator.RandomWordGenerator;

// A simple static method call to get the random word
String randomWord = RandomWordGenerator.getRandomWord()

在幕后,发生了一些事情:

  • We haveRandomWordGenerator包含单词列表的类(randomWords) 在记忆中主题包含各种主题的静态列表的类,例如 表演,国际象棋等在初始化期间,RandomWordGenerator随机向DataMuse API发出HTTP请求话题和更新randomWordsAPI返回的单词列表的值当用户打电话给RandomWordGenerator.getRandomWord()向DataMuse API发起HTTP请求以获取下一个单词列表在单独的线程中从中随机选择的单词randomWords返回列表万一上述HTTP请求失败,系统默认会从主题类本身。 有总比没有好。

注意在单独的线程中 part is pretty important above. Instantiating the HTTP request to DataMuse API 在单独的线程中 allows the RandomWordGenerator.getRandomWord()调用是瞬时的,因为它不需要等待HTTP响应返回。

上面的设计实现了一些目的:

  • 每次致电RandomWordGenerator.getRandomWord()是瞬时的因为我们从randomWords列表已存在于内存中。 我相信这比大多数/所有REST API更好,因为没有HTTP请求的开销。随着时间的推移,确保单词的随机性就像每次致电RandomWordGenerator.getRandomWord()具有更新当前的副作用randomWords用单词列出一个新的随机数话题单词随机性可确保快速多次通话至RandomWordGenerator.getRandomWord(),因为每次通话都会从randomWords清单强大。 If due至any reason the call至DataMuse API fails, then we fall back至the offline 话题s class至get a random word.

如果您想查看源代码或在自己的项目中使用此库,则下面的链接。 请随时在评论中发表您的建议/问题!

GitHub logo Dhiraj072 / random-word-generator

A java library to generate random words

Random word generator

A java library to generate random words.

Usage

Import using Maven

<dependency>
  <groupId>com.github.dhiraj072</groupId>
  <artifactId>random-word-generator</artifactId>
  <version>1.0.0</version>
</dependency>

or using Gradle

compile 'com.github.dhiraj072:random-word-generator:1.0.0'

And use in your code as

// Import the class
import com.github.dhiraj072.randomwordgenerator.RandomWordGenerator;

// Get a random word with a simple static method call
String randomWord = RandomWordGenerator.getRandomWord();

Built With

License

This project is licensed under the MIT License - see the LICENSE.md file for details

from: https://dev.to//dhiraj072/why-i-wrote-my-own-random-word-generator-1nb9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值