随机图片但不重复 [英] random picture but not repeating

查看:237
本文介绍了随机图片但不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在我的项目的一部分并且对VB很新。

i将我的图片切成6等于部分

但是需要随机显示它们,不再按照随机顺序重复相同部分的图片。

例如,pic1,pic2,pic3,pic4,pic5,pic6到==> pic3,pic2,pic6,pic1,pic5,pic4


以下是我的代码的一部分


Dim theImage(0 To 7)As Object

Dim myArray(0到7)As Object


theImage(0)= pic1.Image

theImage(1)= pic2.Image

theImage(2)= pic3.Image

theImage(3)= pic4.Image

theImage(4)= pic5。图片

theImage(5)= pic6.Image

theImage(6)= pic7.Image


For i = 0 To 6步骤1



随机= num.Next(0,7)

循环时(myArray(随机)<> 0 )

myArray(随机)= theImage(i)


接下来我是


while循环中的部分不会工作,因为他们无法解析图像。

我该怎么做呢?我的代码看起来很糟糕。

提前感谢

i am stuck on part of my project and very new to VB.
i had cut my picture into 6 equals part
but need to display them randomly, without repeating the same part of the pic again, in random sequence.
eg, pic1,pic2,pic3,pic4,pic5,pic6 to ==> pic3,pic2,pic6,pic1,pic5,pic4

below are part of my code

Dim theImage(0 To 7) As Object
Dim myArray(0 To 7) As Object

theImage(0) = pic1.Image
theImage(1) = pic2.Image
theImage(2) = pic3.Image
theImage(3) = pic4.Image
theImage(4) = pic5.Image
theImage(5) = pic6.Image
theImage(6) = pic7.Image

For i = 0 To 6 Step 1
Do
random = num.Next(0, 7)
Loop While (myArray(random) <> 0)
myArray(random) = theImage(i)

Next i

the part on the while loop wont work as they cant parse in image.
how do i go about doing it? and my code seems sucky.
thanks in advance

推荐答案

本主题已被覆盖一次或两次,因此您可以使用搜索框找到一些内容。但基本上你需要跟踪使用了哪些数字,而不是再次使用它们。有两种方法可以想到...
  • 创建另一个flags数组。 (很可能是布尔值)表示已使用了哪些图像。在分配映像之前,请检查其相应的标志。如果它已被使用,只需循环并生成另一个随机数。
  • 而不是第二个Object数组,只需将myArray()定义为Long,并为其分配数字而不是图像。然后,您可以扫描此目标阵列,以便在分配每个数据之前查看该数字是否已经在其中,此外,它可能比数组对象使用更少的存储空间。要显示图像,您只需使用此数组中的条目来索引theImage()数组。
This topic has been covered once or twice, so you might find something by using the search box. But basically you need to track which numbers have been used, and not use them again. A couple of ways come to mind...
  • Create another array of "flags" (most likely boolean) to indicate which images have been used. Before allocating an image, check its corresponding flag. If it has been used, just loop around and generate another random number.
  • Instead of a second array of Object, just define myArray() as Long, and assign numbers to it instead of images. Then you can scan this target array to see whether the number is already in it before allocating each one, plus it might use less storage than an array Objects. To display the images, you would just use the entries in this array to index the theImage() array.


哦,好的。

但是如果我们使用long / int?

我们如何将图片分配到数组中,并且在搜索时,帖子说在为图像分配唯一编号时,我该怎么做?
oh, okay.
but how do we assign the picture into the array if we use long/int?
and while searching thru, a post said on assigning a unique number to the image, how do i do it?



但是如果我们使用long / int,我们如何将图片分配到数组中?
but how do we assign the picture into the array if we use long/int?



关键是你没有分配图片,你只需要分配图片的数字。换句话说,第二个数组将以随机顺序保持数字1-6。要显示图片,您只需浏览此数组并使用每个数字作为第一个数组(图像)的索引。

The point was that you don''t assign the pictures, you just assign the numbers of the pictures. In other words, the second array would just hold numbers 1-6, in a random sequence. To show the pictures, you just go through this array and use each number as an index for the first array (theImage).


并在搜索时,一篇文章说,为图像分配一个唯一的编号,我该怎么办?
and while searching thru, a post said on assigning a unique number to the image, how do i do it?



就像我说的,一般的技巧就是简单地保持生成随机数,直到你得到一个在用过的数组中找不到的数字。例如,这里有一个小例子例程来生成一个包含六个非重复数字的数组。它可能不是很优雅,因为我只是在当场弥补它。 (哦,它也从这篇文章中调用RandomNumBetween函数)。

Like I said, the general technique is to simply keep generating random numbers until you get one that you can''t find in the array of used ones. For instance, here''s a little sample routine to generate an array of six non-repeating numbers. It''s probably not very elegant, as I''m just making it up on the spot. (Oh, also it calls the RandomNumBetween function from this post).

展开 | 选择 | Wrap | 行号


这篇关于随机图片但不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆