Jump to content

Random number generator?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
sjshacker93

sjshacker93

    Newbie

  • Members
  • Pip
  • 1 posts
im new to this forum and wanted to kno how to create a random number generator that would pick between 1-7 ? in 3 different places thus creating a slot machine for my project in school.. please help

#2
Alex3k

Alex3k

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
hey, i guess you will be using forms....
I dont feel comfortable giving the code but i can explain how it could be done if you would like...

if you want to me send me a private message with your msn address in it and i will explain to you
Dreamspark really does "Spark Dreams"
---------------------------------------
View my Blog

#3
PsychoCoder

PsychoCoder

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Hey Alex3k since this is a help community based on helping people why not explain it here that way if someone happens across this site with the same question there's somewhere for them to start :)

#4
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
If you are using VB.NET then use Random class: rnd.Next() mod 7 - that will return uniformly distributed random numbers between 0 and 6 inclusive.

If you're using plain VB, then use Rnd function (I think that was the name). Make sure that Randomize function is invoked at the beginning of execution of the program. Again, use Rnd mod 7, which will return numbers between 0 and 6 inclusive.

Random number generators in both languages are based on Linear Congruential Generator (LCG). Note that LCG is used to generate quite large numbers, and then modulo 7 operation is used to reduce the result into the desired range. This method provides longer sequence of numbers before repetition. If you study that Wikipedia page carefully, you'll get to same conclusion.

Make sure that you never combine multiple results of LCG unless you are very sure what you're doing, which can be true only if you are strong in Probability and Statistics fields. For example, formula (LCG + LCG) mod 7 does not produce uniformly distributed values.

#5
Alex3k

Alex3k

    Programmer

  • Members
  • PipPipPipPip
  • 109 posts
Sorry guys, i was only suggesting MSN so i could send pictures...sorry...
i would do something like...

Dim RND as new random
Dim Random1 as integer
Dim Random2 as integer
Dim Random3 as integer

Random1 = rnd.next(0,8)
Random2 = rnd.next(0,8)
Random3 = rnd.next(0,8)

that is to get a general random, VSWE taught me to do something like that.

The (0,8) in the code means you set the bounds for the next.... it gets a number between 0 and 7.


Hope this helps,
Dreamspark really does "Spark Dreams"
---------------------------------------
View my Blog

#6
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts

Alex3k said:

hey, i guess you will be using forms....
I dont feel comfortable giving the code but i can explain how it could be done if you would like...

if you want to me send me a private message with your msn address in it and i will explain to you
Sounds like some type of military grade encryption to me not a simple random function.
Hmm. I don't know VB (won't install) but i'd say you need to say something like:
Textbox1 = Random(1-7)
Thats not acutally code but its pretty close to what you need.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).