473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Random Number Help

ok so im trying to make some easy AI

as the user clicks random buttons the program will keep a running tally of what they choose and then devide it by the amount of times that they have choosen.. thus the program will generate a percent of what he has choosen

now the hard part...
any one know how i can take these percentages and ask the computer to pull a random number using these percentages ... thus having a better chance of choosing something the impooter would have choosen
May 24 '07 #1
4 1351
SammyB
807 Expert 512MB
Let's take a specific case and see how to do it.
Say we have three buttons
1 is pressed 30% of the time
2 is pressed 50% of the time
3 is pressed 20% of the time

So, if we generate a random number between 0 and 100, then
if the number is between 0 and 30, press 1
if the number is between 30 and 80, press 2
if the number is between 80 and 100, press 3

Make sense?
May 25 '07 #2
Killer42
8,435 Expert 8TB
Nice one, Sammy.

Note though, this may require some fairly sophisticated coding. (Hm, sounds like a good topic for a sample in the Articles area). What you need to be wary of, johnny, is just incorrectly "stacking" the percentages. A fairly common method of doing something a certain percentage of the time is like this...
  • Generate a random number between, say, 0 and 100.
  • If the number is <= my percentage, then we have a hit.

This works just fine. But in a case like this where you are choosing between a number of options, the natural inclination might be somethinbg like this (using Sammy's example)...
  • Generate a random number between 0 and 100.
  • If the number is <= 30, press 1
  • Generate a random number between 0 and 100.
  • If the number is <= 50, press 2
  • Generate a random number between 0 and 100.
  • If the number is <= 20, press 3

It may seem as though this would do what you want. But in fact this would change the probabilities quite significantly. For instance, this only allows something like (my math may be wrong) a 35% chance of generating a "2". And if I'm not mistaken, a 7% chance of producing a "3". (I don't have much confidence in my numbers, but hopefully you get the idea.)

So, just be aware of this pitfall.
May 25 '07 #3
thanks guys... im pretty sure i can figure the rest out from here
May 25 '07 #4
Killer42
8,435 Expert 8TB
thanks guys... im pretty sure i can figure the rest out from here
Glad we could help. :)

Let us know how it turns out.
May 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
4
by: anita | last post by:
I had posted this question before, but did not hear from anybody. Can somebody pls help me out. I am creating a table with two fields F1, F2 and F2 has about 50,000 randomly generated alphanumeric...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
3
by: JoelPJustice | last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet...
7
by: alphaLaura | last post by:
Hi all - I'm just new to the group and I hope nobody minds me asking for some help. I currently have an assignment which deals with matrices (more specifically, Gauss-Seidel solving of...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.